分类: C/C++
2008-09-26 00:22:40
|
virtual inheritance
|
In the case of virtual inheritance, only a single occurrence of the base class is maintained (called a subobject) regardless of how many times the class is derived from within the inheritance chain. iostream, for example, contains only a single instance of the virtual ios base class.
Each class object contains a bptr initialized to address its base class table
A meta-language rule is required, dictating that when the language cannot distinguish between a declaration and an expression, it is to be interpreted as a declaration.
The C++ language supports polymorphism in the following ways:
Through a set of implicit conversions, such as the conversion of a derived class pointer to a pointer of its public base type:
shape *ps = new circle();
Through the virtual function mechanism:
ps->rotate();
Through the dynamic_cast and typeid operators:
if ( circle *pc = dynamic_cast< circle* >( ps )) ...
an observant compiler can resolve an invocation of a virtual function through the object at compile time, thus by-passing the virtual mechanism. This can be a significant performance win if the virtual function is defined as inline.
To summarize, polymorphism is a powerful design mechanism that allows for the encapsulation of related types behind an abstract public interface, such as our Library_materials hierarchy. The cost is an additional level of indirection, both in terms of memory acquisition and type resolution. C++ supports polymorphism through class pointers and references. This style of programming is called object-oriented.
C++ also supports a concrete ADT style of programming now called object-based (OB)—nonpolymorphic data types, such as a String class. A String class exhibits a nonpolymorphic form of encapsulation;
There are four characteristics of a class under which the compiler needs to synthesize a default constructor for classes that declare no constructor at all.
Programmers new to C++ often have two common misunderstandings:
That a default constructor is synthesized for every class that does not define one
That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class