C ++带有纯虚函数的多继承问题 [英] C++ A multiple inheritance pproblem with pure virtual functions
问题描述
我创建了一个最小的例子来复制我看到的一个更复杂的类层次结构的问题:
#include < string>
#include< iostream>
类A
{
protected:
virtual
〜A()= 0;
};
inline
A ::〜A(){}
B类:public A
{
public:
virtual
〜B()
{
}
std :: string B_str;
};
class BB:public A
{
public:
virtual
〜BB()
{
}
std :: string BB_str;
};
class C:public A
{
protected:
virtual
〜C()
{
}
virtual
void Print()const = 0;
};
class D:public B,public BB,public C
{
public:
virtual
〜D()
{
}
};
class E:public C
{
public:
void Print()const
{
std :: cout << E< std :: endl;
}
};
class F:public E,public D
{
public:
void Print_Different()const
{
std :: cout<< 不同于E<< std :: endl;
}
};
int main()
{
F f_inst;
return 0;
}
使用 g ++编译--std = c ++ 11 main.cpp
产生错误:
错误:无法将变量'f_inst'声明为抽象类型'F'
F f_inst;
注意:因为以下虚拟函数在'F'中是纯的:
class F:public E,public D
^
note:virtual void C :: Print()const
void Print()const = 0;
^
因此,编译器认为 Print code>是纯虚拟。
但是,我已经指定 Print()
code> class E 。
所以,我误解了一些继承规则。
我的误解是什么,如何解决这个问题?
注意:如果我删除了继承:public D
从类F
。
目前,您的 F
以两种不同的方式派生自 C
。这意味着 F
对象有两个单独的 C
基址,因此有两个实例 C :: Print()
。
目前只能覆盖来自 E
的邮件。
要解决此问题,您必须采取以下选项之一:
- 还覆盖通过
D 或
F :: Print()
来执行 - c>打印
- 使用虚拟继承因此只有一个
C
基础。
,语法调整将是:
class E:virtual public C
pre>
和
D类:public B,public BB, virtual public C
这意味着
D
E
都将具有与其父级相同的C
实例,因此覆盖E: :Print()
覆盖C
的所有类的下游的函数。
有关详细信息,请查找钻石继承问题。另请参见多继承常见问题
I have produced a minimal example to replicate the problem I am seeing with a more complex class hierarchy structure:
#include <string> #include <iostream> class A { protected: virtual ~A() = 0; }; inline A::~A() {} class B : public A { public: virtual ~B() { } std::string B_str; }; class BB : public A { public: virtual ~BB() { } std::string BB_str; }; class C : public A { protected: virtual ~C() { } virtual void Print() const = 0; }; class D : public B, public BB, public C { public: virtual ~D() { } }; class E : public C { public: void Print() const { std::cout << "E" << std::endl; } }; class F : public E, public D { public: void Print_Different() const { std::cout << "Different to E" << std::endl; } }; int main() { F f_inst; return 0; }
Compiling with
g++ --std=c++11 main.cpp
produces the error:error: cannot declare variable ‘f_inst’ to be of abstract type ‘F’ F f_inst; note: because the following virtual functions are pure within ‘F’: class F : public E, public D ^ note: virtual void C::Print() const void Print() const = 0; ^
So the compiler thinks that
Print()
is pure virtual.But, I have specified what
Print()
should be inclass E
.So, I've misunderstood some of the rules of inheritance.
What is my misunderstanding, and how can I correct this problem?
Note: It will compile if I remove the inheritance
: public D
fromclass F
.解决方案Currently your
F
is derived fromC
in two different ways. This means that anF
object has two separateC
bases, and so there are two instances ofC::Print()
.You only override the one coming via
E
currently.To solve this you must take one of the following options:
- Also override the one coming via
D
, either by implementingD::Print()
orF::Print()
- Make
Print
non-pure - Use virtual inheritance so that there is only a single
C
base.
For the latter option, the syntax adjustments would be:
class E : virtual public C
and
class D : public B, public BB, virtual public C
This means that D
and E
will both have the same C
instance as their parent, and so the override E::Print()
overrides the function for all classes 'downstream' of that C
.
For more information , look up "diamond inheritance problem". See also Multiple inheritance FAQ
这篇关于C ++带有纯虚函数的多继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!