C + + constructor semantics
The intervention principles and methods of the compiler during object construction.
How to build Default constructor
Often mentions the default constructor, then when will you synthesize a default consgtructor? When the compiler needs it. Do not synthesize the constructor values perform the actions required by the compiler.
If a class does not have any constructor, but it contains a member object that has the default constructor, the compiler needs to synthesize a defautl constructor for this class, However, this synthetic operation only occurs when the constructor is being called.
Class Foo {public:foo (), foo (int)};
Class Bar {Public:foo Foo; char*str,}; It's not inheritance, it's included.
Void Foo_bar ()
{
barbar;//Bar::foo must be initialized here Bar::foo is a member object, and its class Foo has default constructor, which matches the condition
}
As a result, the Member class object with default constructor causes the class containing this object to synthesize a default constructor, that is, if a class contains classes, However, if the class contains the default constructor, the compiler will construct a default constructor for it without the default constructor.
Base class with default constructor
If a class without any constructors derives from a base class "with Defaultconstructor", then the default of this derived class Constructor will be considered useful (nontrivial) and therefore need to be synthesized. It will call the default constructor of the previous layer base classes
If the designer provides multiple constructors, but none of them are defaultconstructor? The compiler will no longer synthesize the default constructor
It is also important to note that the default constructor described here are constructors that do not have any parameters.
Class with a virtual function
Class with a virtual base class
There are four scenarios in which the compiler synthesizes a default constructor
Therefore, any class that does not have the default constructor defined will be synthesized into one, which is incorrect.
It is also incorrect for the compiler to synthesize a default constructor that explicitly sets each data value in the class.
The build operation of Copy constructor:
This time this function will be called?
Class X {};
x x;
X xx = x;//directly assigned value
If it is X xx;
Xx = x; The assignment operation is called at this time
When an object is the initial value of another object, when the argument is a call to the values, and when the function returns a value call, the copy constructor is used in all three cases
In general, it is a bitwise copy. But in some cases, the compiler will automatically synthesize a copy constructor, what are these situations? If a class contains another class, and the other class has copy construvtor, if it is a virtual mechanism, if there is a virtual base class. In the inheritance mechanism, the parent class has copy constructor
In the following cases, the object must be initialized with the member initialization list:
When a reference member is initialized
When initializing a constmember
When you call a BaseClass constructor, and it has a set of parameters.
When a member class constructor is called, and it has a set of parameters.
In these four cases, initialization must be done in the member initialization list.
Static member variables must be defined outside the class, for what? Because this variable does not belong to any one object, if it is called in the constructor, it will be called whenever an object is initialized, but this variable is not part of any object.
Review:
This summarizes what is default constructor what is copy constructor? At the same time when it will be called, when the compiler will synthesize these functions.
Explore the C + + object model--c++ constructors