Virtual inheritance![]() Virtual inheritance is a C++ technique that ensures only one copy of a base class's member variables are inherited by grandchild derived classes. Without virtual inheritance, if two classes Instead, if classes This feature is most useful for multiple inheritance, as it makes the virtual base a common subobject for the deriving class and all classes that are derived from it. This can be used to avoid the diamond problem by clarifying ambiguity over which ancestor class to use, as from the perspective of the deriving class ( It is used when inheritance represents restriction of a set rather than composition of parts. In C++, a base class intended to be common throughout the hierarchy is denoted as virtual with the Consider the following class hierarchy. struct Animal {
virtual ~Animal() = default; // Explicitly show that the default class destructor will be made.
virtual void Eat() {}
};
struct Mammal: Animal {
virtual void Breathe() {}
};
struct WingedAnimal: Animal {
virtual void Flap() {}
};
// A bat is a winged mammal
struct Bat: Mammal, WingedAnimal {};
As declared above, a call to Bat bat;
Animal& animal = bat; // error: which Animal subobject should a Bat cast into,
// a Mammal::Animal or a WingedAnimal::Animal?
To disambiguate, one would have to explicitly convert Bat bat;
Animal& mammal = static_cast<Mammal&>(bat);
Animal& winged = static_cast<WingedAnimal&>(bat);
In order to call In this case, the double inheritance of This situation is sometimes referred to as diamond inheritance (see Diamond problem) because the inheritance diagram is in the shape of a diamond. Virtual inheritance can help to solve this problem. The solutionWe can re-declare our classes as follows: struct Animal {
virtual ~Animal() = default;
virtual void Eat() {}
};
// Two classes virtually inheriting Animal:
struct Mammal: virtual Animal {
virtual void Breathe() {}
};
struct WingedAnimal: virtual Animal {
virtual void Flap() {}
};
// A bat is still a winged mammal
struct Bat: Mammal, WingedAnimal {};
The The ability to share a single instance of the Additional Example of Several AncestorsThis example to illustrates a case where base class A / \ B C \ / D | E
Here, #include <string>
#include <iostream>
class A {
private:
std::string _msg;
public:
A(std::string x): _msg(x) {}
void test(){ std::cout<<"hello from A: "<<_msg <<"\n"; }
};
// B,C inherit A virtually
class B: virtual public A { public: B():A("b"){} };
class C: virtual public A { public: C():A("c"){} };
// since B,C inherit A virtually, A must be constructed in each child
// B() and C() constructors can be omitted
class D: public B,C { public: D():A("d_a"),B(),C(){} };
// D() constructor omitted
class E: public D { public: E():A("e_a"){} };
// breaks without constructing A
// class D: public B,C { public: D():B(),C(){} };
// breaks without constructing A
//class E: public D { public: E():D(){} };
int main(int argc, char ** argv){
D d;
d.test();
// prints: "hello from A: d_a"
E e;
e.test();
// prints: "hello from A: e_a"
}
Pure Virtual MethodsSuppose a pure virtual method is defined in the base class. If a deriving class inherits the base class virtually, then the pure virtual method does not need to be defined in that deriving class. However, if the deriving class does not inherit the base class virtually, then all virtual methods must be defined. The code below may be explored interactively here. #include <string>
#include <iostream>
class A {
protected:
std::string _msg;
public:
A(std::string x): _msg(x) {}
void test(){ std::cout<<"hello from A: "<<_msg <<"\n"; }
virtual void pure_virtual_test() = 0;
};
// since B,C inherit A virtually, the pure virtual method pure_virtual_test doesn't need to be defined
class B: virtual public A { public: B(std::string x):A("b"){} };
class C: virtual public A { public: C(std::string x):A("c"){} };
// since B,C inherit A virtually, A must be constructed in each child
// however, since D does not inherit B,C virtually, the pure virtual method in A *must be defined*
class D: public B,C {
public:
D(std::string x):A("d_a"),B("d_b"),C("d_c"){}
void pure_virtual_test() override { std::cout<<"pure virtual hello from: "<<_msg <<"\n"; }
};
// it is not necessary to redefine the pure virtual method after the parent defines it
class E: public D {
public:
E(std::string x):A("e_a"),D("e_d"){}
};
int main(int argc, char ** argv){
D d("d");
d.test(); // hello from A: d_a
d.pure_virtual_test(); // pure virtual hello from: d_a
E e("e");
e.test(); // hello from A: e_a
e.pure_virtual_test(); // pure virtual hello from: e_a
}
References
|
Portal di Ensiklopedia Dunia