The difference between Constructor/non-constructor class Foo{public: //foo (): Val (0), next (NULL) {} int val; Foo *next;}; void Foo_bar () { foo bar; if (bar.val && bar.next) { cout << bar.val << Endl; printf ("%p\n", bar.next); cout << "Un initialization" << Endl; } else { cout << "initialization" << Endl; }}
Member Class Object with Default Constructor
(1)ifclass Acontains one or more of theMember-class-object,soclass Aeach one ofConstructoryou must call eachmember-classesof theDefault-constructor;compiler willexpansion of existing constructor, where some code is inserted ,makeUser-codebefore being executed, first call the necessarydefault-constructors.
(2) The C + + language requires that each constructors be called with the "Member-objects in class Life Order " .
Suppose you have the following classes :
Class Dopey{public: Dopey (); ...}; Class Sneezy{public: sneezy (int); Sneezy (); ...}; Class Bashful{public: bashful (); ...}; Class Snow_white{public: Dopey Dopey; Sneezy Sneezy; Bashful bashful; ... private: int mumble;};
If snow_white does not define Default-constructor, There will be a non-trivial-constructor was synthesized. , called sequentially Dopey, Sneezy, bashful of the default-constructors. However, if Snow_white defines the following Default-constructor:
Snow_white::snow_white (): Sneezy (1024x768) { mumble = 2048;}
It will expand to :
Compiler expanded default constructor//c++ pseudo code Snow_white::snow_white (): Sneezy (1024x768) { //Insert Member Class object //Call its Constructor Dopey. Dopey::D opey (); Sneezy. Sneezy::sneezy (); Bashful. Bashful::bashful (); Explicit User-code mumble = 2048;}
Base Class " with Default Constructor"
In a derived class , if member-class-object exists at the same time , its base-class-constructor is called First , and then the member-class-object 's constructor.
Class with a Virtual Function
The following two scenarios :
(1) Class declaration ( or inheritance ) a virtual-function.
(2) class derives from an inherited string chain , which has one or more virtual-base-classes.
The following expansion operations are generated during the compilation period :
[1] a virtual-function-table ( vtbl) will be generated by the compiler ,&NBSP; put in Span style= "Font-family:courier New;" >class virtual-function address
[2] in each of the Class-object in , an additional pointer-member ( aka vptr) will be synthesized by the compiler. , contains the relevant class-virtual-function-table (CLASS-VTBL) the Address .
The following inheritance relationship and code are provided :
void Flip (const Widget &widget) { widget.flep ();}
widget.flip () will be rewritten to use the widget 's vptr and the Flip () entry in VTBL :
Widget.flip () The change in the virtual-throw operation (*widget.vptr[1]). (&widget);
class with a Virtual Base class
Suppose you have the following inheritance relationship and code :
Class X{public: virtual void memfunction () const { cout << "in X" << Endl; }}; Class A:public virtual x{public: void memfunction () const { cout << "in A" << Endl; }}; Class B:public virtual x{public: void memfunction () const { cout << "in B" << Endl; }}; Class C:public A, public b{public: void memfunction () const { cout << ' in C ' << Endl; } };void foo (const X *px) { px-and Memfunction ();} int main () { foo (new X); Foo (new A); Foo (new B); Foo (new C);} /*void foo (const X &rx) { rx.memfunction ();} int main () { foo (X ()); Foo (A ()); Foo (B ()); Foo (C ());} */
compiler cannot pin foo () "through px and access to the x:: memfunction () "The actual offset position ,&NBSP; px ,&NBSP; execute access operation ,&NBSP; so that x:: memfunction () can be deferred until execution is decided
This function is done by placing a pointer (_VBCX [virtual-base-class-x])in each of the virtual base classes in the derived class object . all operations that " access a virtual-base-classvia reference/pointer " can be done through the relevant pointers . the foo () function can be rewritten as follows :
void foo (const X *px) { //__vbcx represents a pointer produced by the compiler, pointing to Virtual-base-class-x px, __VBCX, memfunction ();}
Revisit the construction of Inside the C + + Object Model (2)--default-constructor