Using C++, how do I correctly inherit from the same base class twice?

前端 未结 8 1064
醉酒成梦
醉酒成梦 2020-12-29 10:35

This is our ideal inheritance hierarchy:

class Foobar;

class FoobarClient : Foobar;

class FoobarServer : Foobar;

class WindowsFoobar : Foobar;

class UnixF         


        
8条回答
  •  孤城傲影
    2020-12-29 11:20

    There is nothing "illegal" about having the same base class twice. The final child class will just (literally) have multiple copies of the base class as part of it (including each variable in the base class, etc). It may result in some ambiguous calls to that base classes' functions, though, which you might have to resolve manually. This doesn't sound like what you want.

    Consider composition instead of inheritance.

    Also, virtual inheritance is a way to fold together the same base class which appears twice. If it really is just about data sharing, though, composition might make more sense.

提交回复
热议问题