Constructor
Do you think:
(1) If no default constructor is defined for any class, the compiler will combine them into one;
(2) The Compiler merges the default values of each data member in the social top class;
This is not the case. A default constructor is merged only when the C ++ compiler needs it.
Class with object members
Class {......};
Class B
{
PRIVATE:
A A; // object Member
Int X;
}
Constructor
B: B ()
{
A. A: ();
}
Suppose you have defined a constructor.
A: ()
{
X = 0;
}
The compiler will append the code to it.
A: ()
{
A. A: ();
X = 0;
}
The base class has default constructor (including the constructor to be merged)
Like the first one, you define many constructors, And the compiler will append code to each constructor.
Classes with virtual functions (including pure virtual functions)
Necessary operations of the Compiler
(1) A virtual function table will be generated and the address of the virtual function is placed in it;
(2) A virtual function pointer will be generated, pointing to the virtual function table address;
Therefore, the compiler will set the initial vptr value for each object, place the appropriate virtual function table address, and complete these tasks for the constructor append Code defined by the class.
Virtual inheritance
Class X {publci: int I ;};
Class A: Public Virtual X {public: Int J ;};
Class B: Public Virtual X {public: Double D ;};
Class C: Public A, public B {public: int K ;};
Void Foo (const A * pA) {pa-> I = 1024;} // The position of PA-> X: I cannot be determined during compilation.
Main () {Foo (new A); Foo (New C);} // It may be converted to void Foo (const A * pA) {pa-> _ vbcx-> I = 1024} vbcx is a pointer to Class X, which must be determined in the constructor.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service