Default Constructor in C + +

Source: Internet
Author: User

Recently in the study of the "Insight C + + object Model", see the second chapter of the structural function part, deep feelings, so write down, convenient for everyone. Of course, it is also convenient for me, lest I forget.

1 class test{2public:3      int A; 4 }; 5 int Main () {6    Test T; 7     std::cout<<t.a<<Std::endl; 8 }

As a result, we all know that t.a is not initialized, so it is a random value. So here's the problem, with previous knowledge, when there is no constructor in a class, the compiler generates a default constructor for it, and this default constructor invokes the default constructor for the member variable, and the default constructor for int is to initialize it to 0.

1 int t=int();        // t==0

So why is this happening? The standard of the year was this: The default constructor is generated only when the compiler needs it. In other words, the compiler does not care about the user's "Bird Thing", the compiler does not care whether the user needs, he only cares about himself ... Try the following code:

Test t=Test (); Std::cout<<t.a<<std::endl;    // Get 0

At this point, the constructor for Test was explicitly called, but the test has no constructor, and the compiler needs a constructor at this time, so he created one. So a will get the default value.

Why should there be such unreasonable design? Lippman not mentioned in the book. But I guess it's supposed to be compatible with C code, consider this:

 1  struct   test{ 2   int   A;  3  };  4   Main () { 5  struct   Test T;  6  printf ( " %d\n   " ,t.a);  7 } 

If the default constructor for test is generated and causes a to be initialized to 0, then C + + will output 0. However, there is no concept of constructors in C, so any value will be output. Early C + + is one of the first to be good compatible with C code, so C + + compiler in order to better compatible with C, the choice of this seemingly incredible method. (PS: In my humble opinion, not many people should be used directly in the case of uninitialized parameters, so the probability of incompatibility should be low)

So under what circumstances will the C + + compiler give the Class A default constructor? Very simple, the book said four kinds of situations:

1. If the member object has a default constructor

2. This class takes on inheritance, and the base class has a default constructor

3. This class has a virtual function

4. This kind of virtual inheritance from other classes

I boil it down to the impossible situation in C code. In the case of C code, the compiler does not produce a default constructor. The following are four cases in turn.

Consider the first type first:

 class   test1{ public  :  int   A;};  class   test2{ public     : Test1 T;  int   b;};  int      main () {test2 T;      Std::cout  <<t.t.a<<std::endl; //      print 0  std::cout<<t.b<<std::endl; // uninitialized } 

What about the initialization? Why is t only initialized, b why not? Note that the default constructor that is synthesized only satisfies the needs of the compiler, not the needs of the program (selfish). In other words, he only initializes a member object with a default constructor (Ps:int is at least not an object.) In addition, one might ask why a member object that has a constructor but not a default constructor is initialized ... Think of this constructor being shown to call AH). and is bound to initialize a member object with a default constructor. Other words

class test2{public:    //...     test2 () {        b=0;    }};

Even so, T is initialized, and this code is equivalent to:

class test2{public:    //...     test2 (): Test1 () {        b=0;    }};

Not finish more

Default Constructor in C + +

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.