C ++ Ein Mehrfachvererbungsproblem mit reinen virtuellen Funktionen
Ich habe ein minimales Beispiel erstellt, um das Problem, das ich sehe, mit einer komplexeren Klassenhierarchiestruktur zu wiederholen:
#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;
}
Kompilieren mitg++ --std=c++11 main.cpp
erzeugt den Fehler:
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 denkt der Compiler, dassPrint()
ist rein virtuell.
Aber ich habe angegeben, wasPrint()
sollte in @ seclass E
.
Also, ich habe einige der Vererbungsregeln falsch verstanden.
Was ist mein Missverständnis und wie kann ich dieses Problem beheben?
Hinweis: Wird kompiliert, wenn ich die Vererbung entferne: public D
vonclass F
.