Will the compiler generate a default constructor for classes without a constructor? -- Sighs at the frequency of Chinese textbooks

Source: Internet
Author: User

 

 

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)

 

 
 
  1. #include <iostream>  
  2.  
  3. using namespace std;  
  4.  
  5. class BaseClass 
  6. public: 
  7.     int a; 
  8.     int b; 
  9.     //virtual void showClassInfo(){}; 
  10.     //BaseClass(){} 
  11. };  
  12.  
  13. class DerivedClass:public BaseClass 
  14. public: 
  15.     int c; 
  16. };  
  17.  
  18. int main(int *argc,char **argv) 
  19.     BaseClass baseClass; 
  20.     //cout<<baseClass.a<<" "<<baseClass.b<<endl; 
  21.     cout<<sizeof(baseClass)<<endl; 
  22.     DerivedClass derivedClass; 
  23.     //cout<<derivedClass.c<<endl; 
  24.     cout<<sizeof(derivedClass)<<endl; 
  25.     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

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.