Function specifier
virtual and inline are function specifier.
They can appear before function only.{as per my understanding}.
Then,In following code what 开发者_高级运维is virtual?
class Base
{
//
};
class Derived :virtual public Base
{
};
This is virtual inheritance.
If your question is about the wording in the standard, then you must have misunderstood it. It is true that the list of various function specifiers includes the keyword virtual
as one possible function specifier. However, it doesn't work in other direction: the keyword virtual
is not restricted to being the function specifier only. It has other use(s). You have found an example of that - it can be used to declare virtual base classes.
$10.1/4- "A base class specifier that contains the keyword virtual, specifies a virtual base class. For each distinct occurrence of a nonvirtual base class in the class lattice of the most derived class, the most derived object (1.8) shall contain a corresponding distinct base class subobject of that type. For each distinct base class that is specified virtual, the most derived object shall contain a single base class subobject of that type."
So given the hierarchy
struct A{};
struct B : virtual A{};
struct C : virtual A{};
struct D : B, C{};
D d;
A 'd' object has only one 'A' subobject i.e. the constructor of 'A' is called only once, and that too before any other constructor is run.
$12.6.2/5 - "Initialization shall proceed in the following order:
— First, and only for the constructor of the most derived class as described below, virtual base classes shallbe initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base class names in the derived class base-specifier-list.
— Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
— Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
— Finally, the body of the constructor is executed. [Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. ]
It is virtual inheritance, look here for an explanation.
The common usage is, if you have a class A inheriting from 2 classes B and C, which in turn inherit from the same ancestor D. This is problematic with normal inheritance, since A would contain two instances of D, so which one should be used. Using virtual inheritance, e.g. C inheriting virtual from D, the address of the D instance in the C-part of A is found in the vmt, so it can point to the same instance that the B-part is using
virtual inheritence its used to specify how inheritence works when there is multiple inheritence. essentially it allows you to have 'diamond' inhertitence graphs as opposed to non virtual which would give you a tree, with multiple bases clases at the leaves (raising potenetial ambiguity problems).
精彩评论