我有以下使用多重继承的代码。目的是在派生类中使用两个接口作为一个接口:
struct InterfaceA
{
virtual void register_stuff();
virtual void on_state_changed( const State state ) = 0;
};
struct InterfaceB
{
virtual void register_stuff();
virtual void on_state_changed( const State state ) = 0;
};
struct Derived : InterfaceA, InterfaceB
{
void register_stuff() override
{
InterfaceA::register_stuff();
InterfaceB::register_stuff();
}
void on_state_changed( const State state ) override
{
// how can I know who is responding?
}
};
注册接口将导致异步调用on_state_changed
。是否可以辨别哪个接口在调用它?
您必须在中间添加一层以消除歧义。这里有一个小实用程序,可以动态创建它们:
template<class Inteface>
struct disambiguate : Interface {
void on_state_changed( const State state ) override final {
on_state_changed(state, this);
}
virtual void on_state_changed( const State &state, disambiguate* ) = 0;
};
仅此而已。然后是在此实用程序中定义类的问题:
struct Derived : disambiguate<InterfaceA>, disambiguate<InterfaceB>
{
void register_stuff() override
{
InterfaceA::register_stuff();
InterfaceB::register_stuff();
}
void on_state_changed( const State &state, disambiguate<InterfaceA>* ) override
{
// Called for A
}
void on_state_changed( const State &state, disambiguate<InterfaceB>* ) override
{
// Called for B
}
};
我使用了另一个参数和重载来使其模板化,但技术本身也可以通过写出类并使用新名称调用虚拟函数来完成。关键是使原始虚拟调用(通过接口指针(到达调用消除歧义函数的短速。
或者,可以直接在 Derived 的代码中为处理程序提供单独的实现:
struct Derived : InterfaceA, InterfaceB
{
void register_stuff() override
{
InterfaceA::register_stuff();
InterfaceB::register_stuff();
}
void InterfaceA::on_state_changed( const State state ) override
{
// responding A
}
void InterfaceB::on_state_changed( const State state ) override
{
// responding B
}
};
编辑:不幸的是,此解决方案是非标准的,仅受Visual C++编译器支持。
我也在考虑使用模板来消除歧义,但我相信@StoryTeller的答案更优雅。
struct InterfaceA
{
virtual void register_stuff(); // calls on_state_changed<InterfaceA>()
template <typename Interface>
virtual void on_state_changed( const State state ) = 0;
};
struct InterfaceB
{
virtual void register_stuff(); // calls on_state_changed<InterfaceB>()
template <typename Interface>
virtual void on_state_changed( const State state ) = 0;
};
struct Derived : InterfaceA, InterfaceB
{
void register_stuff() override
{
InterfaceA::register_stuff();
InterfaceB::register_stuff();
}
template <typename Interface>
void on_state_changed( const State state ) override
{
// how can I know who is responding?
// : "Interface" is responding
}
};