When I had nothing to do during the holidays, I downloaded a master's VC ++ video materials. When talking about C ++, it is said that if the programmer does not define the default constructor, the compiler will automatically generate a default constructor for us. It is not surprising that many programmers have known this mistake. However, a programmer with so many years of programming experience can't help but make me dumb.
In fact, the programming language is no different from any software we use, such as Photoshop and AutoCAD. The only difference is that the programming language we use is based on the compiler, and the application software is based on our programming language.
Since the software we use is based on the compiler, it is very important to understand what the compiler has done for us and under what circumstances. This is just like Photoshop will generate some basic images for you, such as Rectangles and triangles, instead of some beautiful pictures.
Section 12.1 in C ++ annotated Reference Manual (ARM) [ellis90] tells us: "Default constructors... generated by the compiler as needed ".
In fact, the default constructor is also divided into two types: Useful and useless.
The so-called useful standard is that the default constructor will perform some initialization operations for our class. So useless will not do any work, so it has no meaning for our class. Therefore, the default constructor is a useful default constructor. Its English name is nontrivial default constructor.
So when will the compiler generate nontrivial default constructor for us? There are four situations:
① If a member object in a class has nontrivial default constructor, the compiler will generate nontrivial default constructor for our class.
So what is the reason for the compiler to do this?
The answer is that the class member object has nontrivial default constructor, so the compiler needs to explicitly call the nontrivial default constructor of this class member object. To explicitly call nontrivial default constructor of a class member object, the compiler needs to synthesize some code to call it. But remember, the nontrivial default constructor synthesized by the compiler only calls the default constructor of the class member object, and does not initialize other variables in our class.
That is to say, if you want to initialize a variable other than a class member variable, such as an int or a string, you must define the default constructor to initialize these variables. The compiler will extend the default constructor you define to call the nontrivial default constructor of the class member object.
② If the base class of a derived class has nontrivial default constructor, the compiler will synthesize a nontrivial default constructor for the derived class.
The compiler must explicitly call the default constructor of the base class when the derived class is merged.
③ How can a class implicitly contain any virtual function table (or vtbl), pointer member (or vptr ).
The reason for the compiler to do this is simple: because the vtbl or vptr needs to be implicitly synthesized by the compiler (implicit), the compiler then places the merging action in the default constructor. Therefore, the compiler must generate a default constructor to complete these operations.
So if your class has any virtual function, the compiler will synthesize a default constructor for you.
④ If a virtual class inherits from other classes.
The reason for the compiler to do this is similar to ③: Because Virtual inheritance requires maintaining something similar to a pointer, it can dynamically determine the memory address (different compilers have different implementations for virtual inheritance ).
In addition to the above four situations, the compiler will not generate Default constructors for our classes.
Therefore, do not take it for granted in programming. You must understand what is done by the compiler and what needs to be done by the programmer. Just as the resources occupied by the heap need to be released by the programmer, and the stack space is managed by the compiler.
Only in this way can we compile code with higher quality.