After reading the "inside c ++ object model", I will verify it by myself. I still remember the C ++ textbooks published by Tsinghua University Press in my sophomore year. It took me two weeks to finish reading them. At that time, I didn't have any object-oriented ideas, not to mention the project experience. I feel that the whole C ++ is to expand the struct of C, and add the dot polymorphism mechanism and generic programming. In fact, at that time, I was not very clear about the concepts of polymorphism and generic programming, but only about virtual and templete ). Haha .. Now it seems very conceited. In fact, this book is simple, but I have read it many times. When talking about constructor, the Book clearly states: "When a class has no constructor, then the compiler will generate a default constructor for it ". I have never read the copyright of Lippman, maybe I will always think that this is an everlasting truth. Lippman commented on this understanding of many programmers:
"C ++ beginners generally have two common misunderstandings:
1. If no default construtor is defined for any class, it will be merged;
2. The default constructor synthesized by the compiler will explicitly set every data member in the class"
Look at the first article. Isn't that the textbook writer a beginner in C ++? Haha ..
Lippman also clearly explains that the compiler will automatically generate a default constructor only when necessary. When is necessary: it is necessary to meet the following four points:
1. class member object with default constructor;
2. base class with default constructor;
3. With virtual function;
4. Has vitrual base class.
Lippman does not provide a clear verification example for these four points, because to verify whether a compiler has synthesized the default constructor for a class, except for the assembly code from the perspective of the assembly language, it seems that there are no other methods .. I lamented that my compilation was weak and could not be analyzed from the compilation perspective. Fortunately, I accidentally found a verification method in VS2008. Let's take a look at the following code: This simple code can verify the above four terms)
- #include <iostream>
-
- using namespace std;
-
- class BaseClass
- {
- public:
- int a;
- int b;
- //virtual void showClassInfo(){};
- //BaseClass(){}
- };
-
- class DerivedClass:public BaseClass
- {
- public:
- int c;
- };
-
- int main(int *argc,char **argv)
- {
- BaseClass baseClass;
- //cout<<baseClass.a<<" "<<baseClass.b<<endl;
- cout<<sizeof(baseClass)<<endl;
- DerivedClass derivedClass;
- //cout<<derivedClass.c<<endl;
- cout<<sizeof(derivedClass)<<endl;
- return 0;
- }
The result will be: 8 12
This proves that the nonstatic data member of the class is correctly applied for memory space. If we remove:
// Cout <baseClass. a <"" <baseClass. B <endl;
This line of comments will contain runtime error. the prompt message is: the variable "baseClass" is being used without being initialized. (baseClass is not initialized). I assume this sentence is intended to indicate that this exception is because baseClass does not set the default constructor, not because baseClass is not set. a And baseClass. B assigns an initial value. To verify this, I add a constructor that has nothing to do and remove it.
// BaseClass (){}
The comments of this line will produce the following results:
-858993460-858993460
8
12
At this time, the program has no exceptions. Verify that my previous assumptions are correct. The constructor does not initialize the data member of baseClass, but runs properly.
If we comment out this in the above Code: note that the BaseClass constructor is commented out at this time)
// Virtual void showClassInfo (){};
If the comments of this line of code are removed, the program will still run normally and the result will be:
-858993460-858993460
12
16
At this time, the baseClass implies a vptr pointing to the virtual function table, which increases the baseClass layout by 4 bytes ). For the C ++ object layout, you can see my previous blog post "Click here ". This verifies the third article Lippman said. To verify article 4, we set this line in the above Code:
// Cout <derivedClass. c <endl;
After the annotation is removed, a runtime error occurs again. the following message is displayed: the variable "derivedClass" is being used without being initialized. (derivedClass is not initialized ). It is proved that the compiler does not synthesize the default constructor by derived class inherited from non-vitual. What will happen to normal virtual inheritance? Try again
Class DerivedClass: public BaseClass
Change to the following:
Class DerivedClass: virtual public BaseClass
Note: The comments of // cout <derivedClass. c <endl; are removed)
The program will run normally again, and the result is:
8
-858993460
16
This verifies article 4.
So far, there is no need to continue the verification methods for the first and second articles.
Finally, I have to lament the difference between Lippman, a foreign textbook writer, and domestic textbook writers ..
This article is from the "Chief" blog, please be sure to keep this source http://clement.blog.51cto.com/2235236/767263