使用C ++,如何正确地两次从同一个基类继承? [英] Using C++, how do I correctly inherit from the same base class twice?
问题描述
这是我们理想的继承层次结构:
This is our ideal inheritance hierarchy:
class Foobar;
class FoobarClient : Foobar;
class FoobarServer : Foobar;
class WindowsFoobar : Foobar;
class UnixFoobar : Foobar;
class WindowsFoobarClient : WindowsFoobar, FoobarClient;
class WindowsFoobarServer : WindowsFoobar, FoobarServer;
class UnixFoobarClient : UnixFoobar, FoobarClient;
class UnixFoobarServer : UnixFoobar, FoobarServer;
这是因为我们的继承层次结构会尝试从Foobar
继承两次,因此,编译器会抱怨Foobar
的任何成员上的引用不明确.
This is because the our inheritance hierarchy would try to inherit from Foobar
twice, and as such, the compiler would complain of ambiguous references on any members of Foobar
.
允许我解释为什么我想要一个如此复杂的模型.这是因为我们希望从WindowsFoobar
,UnixFoobar
,FoobarClient
和FoobarServer
访问相同的变量.这不会有问题,只有我想对以上任意组合使用多重继承,以便我可以在任何平台上使用服务器/客户端功能,也可以在客户端或服务器上使用平台功能.
Allow me to explain why I want such a complex model. This is because we want to have the same variable accessible from WindowsFoobar
, UnixFoobar
, FoobarClient
, and FoobarServer
. This wouldn't be a problem, only I'd like to use multiple inheritance with any combination of the above, so that I can use a server/client function on any platform, and also use a platform function on either client or server.
我忍不住觉得这是一个多重继承的普遍问题……我是从完全错误的角度来解决这个问题吗?
I can't help but feel this is a somewhat common issue with multiple inheritance... Am I approaching this problem from completely the wrong angle?
此外,请考虑我们可以使用#ifdef
来解决此问题,但是,这样会产生非常丑陋的代码,如下所示:
Also, consider that we could use #ifdef
to get around this, however, this will tend to yield very ugly code like such:
CFoobar::CFoobar()
#if SYSAPI_WIN32
: m_someData(1234)
#endif
{
}
...好吧!
对于那些想深入了解此问题背景的人,我真的建议略读适当的邮件列表线程.在第3个帖子周围,事情开始变得有趣起来.另外,您还可以进行相关代码提交在此处查看有问题的真实代码.
For those who want to read more into the background of this issue, I really suggest skimming over the appropriate mailing list thread. Thing start to get interesting around the 3rd post. Also there is a related code commit with which you can see the real life code in question here.
推荐答案
它可以正常工作,尽管您将获得基类Foobar
的两个副本.要获得一个副本,您需要使用虚拟继承.在此处上阅读.
It would work, although you'd get two copies of the base Foobar
class. To get a single copy, you'd need to use virtual inheritance. Read on multiple inheritance here.
class Foobar;
class FoobarClient : virtual public Foobar;
class FoobarServer : virtual public Foobar;
class WindowsFoobar : virtual public Foobar;
class UnixFoobar : virtual public Foobar;
但是,与多重继承相关的问题很多.如果您真的想展示模型,为什么不让FoobarClient
和FoobarServer
在构造时引用Foobar
,然后使用Foobar& FoobarClient/Server::getFoobar
?
However, there are many problems associated with multiple inheritance. If you really want to have the model presented, why not make FoobarClient
and FoobarServer
take a reference to Foobar
at construction time, and then have Foobar& FoobarClient/Server::getFoobar
?
组合通常是摆脱多重继承的一种方法.现在举个例子:
Composition is often a way out of multiple inheritance. Take a example now:
class WindowsFoobarClient : public WindowsFoobar
{
FoobarClient client;
public:
WindowsFoobarClient() : client( this ) {}
FoobarClient& getClient() { return client }
}
但是在使用此功能时,必须要小心在构造函数中.
However care must be taken in using this in the constructor.
这篇关于使用C ++,如何正确地两次从同一个基类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!