【发布时间】:2021-12-19 00:29:40
【问题描述】:
这就是我所拥有的:
class Foo;
class Bar;
class FooBar
{
public:
FooBar( Foo &f, void ( Foo::* signal )( FooBar* ) )
: m_Foo(&f)
, fp_Foo(signal) {};
FooBar( Bar &b, void ( Bar::* signal )( FooBar* ) )
: m_Bar(&b)
, fp_Bar(signal) {};
protected:
void ( Foo::*fp_Foo )( FooBar* );
void ( Bar::*fp_Bar )( FooBar* );
private:
Foo *m_Foo{nullptr};
Bar *m_Bar{nullptr};
};
这就是我想要的:
class Foo;
class Bar;
class FooBar
{
public:
FooBar( Foo &f, void ( Foo::* signal )( FooBar* ) )
: m_Foo(&f)
, fp(signal) {};
FooBar( Bar &b, void ( Bar::* signal )( FooBar* ) )
: m_Bar(&b)
, fp(signal) {};
protected:
void ( <???>::*fp )( FooBar* ); // Deduplicated
private:
Foo *m_Foo{nullptr};
Bar *m_Bar{nullptr};
};
我只想要一组成员函数指针,它们可以同时代表Foo 和Bar。
不使用模板可以吗?
谢谢。
【问题讨论】:
-
QList 不是 QObject。子类 QObject 并用列表组成子类。
-
我认为从这个问题中删除 Qt 将有助于使其更加清晰。您是在询问使用派生类获取的成员数据指针是否与从基类获取的指针相同?
-
@FrançoisAndrieux,OP 有一个 XY 问题。 Qt 就在标签中。并且已经给出了解决方案。
-
@FrançoisAndrieux 是的,这是真的。我将编辑问题并取出 Qt cruft,因为它已经浪费了某人的时间。
Are you asking if a member data pointer taken using a derived class the same as that pointer taken from the base class?没有。 -
@Anon 我认为成员函数指针不可能做到这一点。该类是其类型的一部分,它们不可相互转换。你可以试试
std::function<void(FooBar*)>。否则,您可以尝试使用模板。
标签: c++ casting c++17 function-pointers member-function-pointers