Analysis of constructor
Author: normalnotebook
The constructor converts a bunch of logical contacted storage unit units into a live object. It may be used to initialize internal data members of the object, or it is also possible to assign resources (memory, file, semaphore, etc.). Construction function It is a member function with the same name as the class. The constructor is automatically called when the object is created. The general expression is: x :: x ();
Class X
{
PUBLIC:
X ();
X (int);
protected:
Int value;
}
The following rules are based on the above examples;
Rules 1: Statement Rules I:
1. There is no declaration that the default (default) constructor is declared, and there is no other constructor, legal.
2. If only declare the default (default) constructor, legal.
3. If the default (default) constructor is declared, it is not public, error.
4. Declare the default (default) constructor, also declare the constructor with the default parameters. Error. Because there is a buzzing constructor call.
The constructor without any parameters is called the default (default) constructor. If it is contrary to the third article in the rule, it will cause compilation errors. It must be declared as public type. (Thinking: why?). If declare The constructor of the default parameter. Name, X (int = 0); function. Then you can call X a; if you also declare x (); will cause compilation errors. Because the compiler can't know which function is connected, Is x (); or x (int = 0); function? So it is impossible to occur.
Class X
{
PUBLIC:
X ();
X (int = 0); // error. These two can only appear. I can't have you.
protected:
Int value;
}
Also pay attention to the following two cases:
A. If there is no declaration default constructor, one or more constructor requirements are required.
Then use X a; when you create an object A, it will cause compilation errors. Because it is too small. You can only create X a (parameter list) in this case;
B. If you do not define the default constructor, one or more constructor requirements that require the arguments are defined, and the default parameters are default.
Then can solve the error in A.
Rule 2: Declaration Rules II:
1. Constructor cannot specify the return value type and return value.
2. Can't be declared as virtual functions
3. You can overload the constructor.
The corresponding destructor can be declared as a virtual function, but it cannot be overchatrifted. A class can only have a destructor and cannot pass the paced function to the constructor.
Rules 3: Initialization rules:
1. Class data members cannot initialize when defined.
2. You must initialize in the constructor of the class or set them after the object is created.
Different from the Java program, the Java program can assign the initial value to the data member in the declaration class, but in C is not allowed. You must initialize in the constructor of the class. That is, according to the member initialization table to initialize the data member As x :: x (): value (3) {}; for any type of const and reference data members, must be initialized in the member initialization table, otherwise, it will cause compilation time errors. And for other internal types Assign initial values in the constructor.
Rules 4: Call sequence rules:
1. Call a constructor automatically when you create an object.
2. When you create a derived class object, first perform the constructor of the base class, then execute the constructor of the derived class member object, and finally perform the constructor of the derived class. (The call rules of the destructor are opposite to the constructor).
3. If there is no display definition constructor, the compiler will add a default constructor to this class, but the default constructor does not do any initialization.
Rule 5: Define Object Rules:
Only when there is no declaration of constructor or declares that the default constructor is not declared, or if we declare that the default parameter is not specified, we can define the active class object.
X a (); // Compile error is interpreted by the compiler to define a function without parameters, returning a X-type object. The correct statement of the object initialized by the default constructor is to remove parentheses. Instead of calling the default constructor .
Rule 6: Copy constructor:
1. When passing by numerical delivery, the copy constructor should be called by the value returns or explicitly copy an object.
2. Copy constructor features marked as: x :: x (const X &).
3. Although the parameters of the copy constructor are not constant, it must be referenced. (Thinking, why?) If there is anything wrong, please ask everyone to criticize view .NORMALNOTEBOOK @ 126.com