Previous: http://www.bkjia.com/kf/201201/115850.html
17.3.5 virtual inheritance
In C ++, virtual inheritance is used to solve such problems. Virtual inheritance is a mechanism in which a class indicates through virtual inheritance that it wants to share the state of its virtual base class. Under virtual inheritance, for a given virtual base class, no matter how many times the class appears as a virtual base class in the derived hierarchy, only one shared base class sub-object is inherited. The shared base class sub-object is called the virtual base class ).
Set the virtual base class by including the keyword virtual in the derived list.
Namespace andsoft
{
Namespace Animal
{
Class ZooAnimal {
};
Class Endangered {
};
Class Bear: virtual public ZooAnimal {
};
Class Raccon: virtual public ZooAnimal {
};
Class Panda: public Bear, public Endangered, public Raccon {
Public:
Panda (): Bear (), Endangered (), Raccon (){}
};
}
}
Namespace andsoft
{
Namespace Animal
{
Class ZooAnimal {
};
Class Endangered {
};
Class Bear: virtual public ZooAnimal {
};
Class Raccon: virtual public ZooAnimal {
};
Class Panda: public Bear, public Endangered, public Raccon {
Public:
Panda (): Bear (), Endangered (), Raccon (){}
};
}
} In fact, specifying the inheritance of the intermediate base class as virtual inheritance rarely causes any problems. Generally, the class hierarchy that uses virtual inheritance is designed by one person or a project design group at a time. A class developed independently rarely needs one of its base classes to be a virtual base class, in addition, developers of new base classes cannot change existing layers.
17.3.6 virtual base class declaration
Use the virtual keyword to modify the Declaration and specify the base class as derived from the virtual inheritance.
Class ZooAnimal {
};
Class Endangered {
};
Class Bear: virtual public ZooAnimal {
};
Class Raccon: virtual public ZooAnimal {
};
Class Panda: public Bear, public Endangered, public Raccon {
Public:
Panda (): Bear (), Endangered (), Raccon (){}
};
Class ZooAnimal {
};
Class Endangered {
};
Class Bear: virtual public ZooAnimal {
};
Class Raccon: virtual public ZooAnimal {
};
Class Panda: public Bear, public Endangered, public Raccon {
Public:
Panda (): Bear (), Endangered (), Raccon (){}
}; Specifying virtual derivation only affects classes derived from the specified virtual base class. In addition to the objects of the derived classes, it is also a statement about the relationship between the derived classes and their future Derived classes.
Virtual specifiers indicate the desire to share a single instance of the specified base class in a derived class.
Any class that can be specified as the base class can also be specified as a virtual base class. The virtual base class can contain any class elements normally supported by non-virtual base classes.
1. supports regular conversion to the base class
Even if the base class is a virtual base class, you can usually manipulate the object of the derived class through a pointer of the base class type or reference.
2. virtual base class member visibility
The use of multiple inheritance levels of virtual functions causes less ambiguity than the absence of virtual inheritance.
You can directly access members in the shared virtual base class without ambiguity. Similarly, if you only redefine members from the virtual base class along a derived path, you can directly access the redefinition member. In the case of non-virtual derivation, both types of access may be ambiguous.
If x is a member of the virtual base class in a path, and x is a member of the derived class in another path, there is no ambiguity either-the priority of a particular derived class instance is higher than that of a shared virtual base class instance.
Special initialization meanings of 17.3.7
In virtual derivation, the constructor of the lowest-layer derived class initializes the virtual base class.
Although the virtual base class is initialized by the lowest-layer derived class, any class that directly or indirectly inherits the virtual base class must also provide its own initialization type for the base class. As long as you can create an independent object of the virtual base class derived class type, the class must initialize its own virtual base class. These initialization methods are only used when creating intermediate objects.
2. constructor and destructor order
No matter where the virtual base class appears in the inheritance level, the virtual base class is always constructed before the non-virtual base class is constructed.
From xufei96's column