1. function: Creates (build objects) objects, changes a series of random memory bits to objects, and allocates resources (such as memory, files, semaphores, and sockets ), "ctor" is a typical abbreviation of constructor.
2. Assume that list is a class name, list X andThe difference between list X (): the former declares a list object, and the latter is a function that returns the list type.
3. Can I call another constructor in one constructor? The answer is no.
Assume that the class fro has two constructors: FOO: Foo (char X) and foo: Foo (char X, int y). The following code
Class Foo {
Public:
Foo (char X );
Foo (char X, int y );
...
};
Foo: Foo (char X)
{
...
Foo (x, 0 );// This line does not help initializeThisObject !!To initialize a temporary (temporary amount), local
Object (Not This), It immediately destructs that temporary when control flows over,
...
}
You can also combine the two constructors by default parameters.
Class Foo {
Public:
Foo (char X, int y = 0 );// This line combines the two Constructors
...
};
If no default parameter is available, I can share the public code in the private init function, for example:
Class Foo {
Public:
Foo (char X );
Foo (char X, int y );
...
PRIVATE:
Void Init (char X, int y );
};
Foo: Foo (char X)
{
Init (x, INT (x) + 7 );
...
}
Foo: Foo (char X, int y)
{
Init (x, y );
...
}
Void FOO: Init (char X, int y)
{
...
}
Do not try to use it in placement new. Some people think it is okay. New (this) Foo (x, INT (x) + 7) in Foo: Foo (char). This is absolutely incorrect. It affects the building bit of the object (constructed bits ).
4. default constructor parameters can be left blank or default parameters. For example:
Class Fred {
Public:
Fred ();// Default constructor: can be called with no ARGs
...
}; Or
Class Fred {
Public:
Fred (INT I = 3, Int J = 5 );// Default constructor: can be called with no ARGs
...
};
5. Which constructor will be called when an object array is created:
If no default constructor exists, an error occurs when creating an object array. For example:
Class Fred {
Public:
Fred (int I, Int J );BytesAssume there is noDefault constructor
...
};
Int main ()
{
Fred a [10];BytesError:FredDoesn' t have a default constructor
Fred * P = new Fred [10];BytesError:FredDoesn' t have a default constructor
...
}
If you use STD: vector <Fred> , You do not need to use the default constructor. For example:
# Include <vector>
Int main ()
{
STD: vector <Fred> A (10, Fred (5, 7 ));BytesThe 10FredObjects in STD: Vector AWill be initialized Fred (5, 7)
...
}
You can also initialize Arrays: You can also use placement new to manually initialize array elements.
Class Fred {
Public:
Fred (int I, Int J );BytesAssume there is noDefault constructor
...
};
Int main ()
{
Fred a [10] = {
Fred (5, 7), Fred (5, 7), Fred (5, 7), Fred (5, 7), Fred (5, 7 ),// The 10FredObjects are
Fred (5, 7), Fred (5, 7), Fred (5, 7), Fred (5, 7), Fred (5, 7)// Initialized using Fred (5, 7)
};
...
}
In general, we use vector to replace arrays.
6. How to Use initialization lists and assignment in Constructors)
Using the initialization list in the constructor is more efficient than assigning values. The latter has a temporary variable and the overhead of creating and destroying the temporary variable. But there is little difference between the two built-in data types (INT, float, etc.
Another case is that the member objects in the construction will be fully constructed by the default constructor, and some memory or files in the default state will be allocated, in this way, if expression or copy fails in the construction, resources cannot be released or files cannot be closed.
In the following cases, it is not easy to use the initialization list: the class has two constructors and the data member needs to be initialized in different order, or two data members need to reference themselves, or the data member must reference this object, or initialize this member before throwing an exception.
7. Can constructors use the this pointer? Yes, but use it with caution, even in the initialization list.
Applicable situations: Constructors (or constructors called by constructors) it can reliably access the data members declared in the base class and/or the data members declared in the class to which the constructor belongs. This is because all the data members are guaranteed to have been fully established when the constructor starts execution.
The function body of the constructor (or the function called by the constructor) cannot call down the virtual function that is redefined by the derived class. No matter how you call the virtual member function: explicitly useThisPointer (for example,This-> method ()), Implicit useThisPointer (for example,Method ()), Or evenThisObject To call other functions to call the virtual member function. Cause: during the execution of the base class constructor, the derived class object has not yet been generated.
The following situations are sometimes feasible:ThisThe initialization program of any data member of the object to another data member. You must ensure that the data member has been initialized. Its advantage is that it does not rely on the compiler, but you must know some language rules (for example, the base class sub-objects are initialized first (if there are multiple and/or virtual inheritance, query this order !), The data members defined in the class are initialized according to the declared order in the class). If you do not know, do not use this pointer.
8. Named constructor idiom ):
The function is to differentiate multiple constructors.
Structure: place the structure in private or protected to provide a publicStaticMethod. There is such a static method for each method of constructing different objects. Example:
Class Point {
Public:
Point (float X, float y );// Rectangular coordinates
Point (float R, float );// Polar coordinates (radius and angle)
// Error: overload is ambiguous: Point: Point (float, float)
};
Int main ()
{
Point P = point (5.7, 1.2 );// Ambiguous: which coordinate system?
...
}
The solution is to use named constructor idiom.
# Include <cmath>// To get Sin () And Cos ()
Class Point {
Public:
Static point rectangular (float X, float y );// Rectangular coord's
Static point polar (float radius, float angle );// Polar Coordinates
// TheseStaticMethods are the so-called "named constructors"
...
PRIVATE:
Point (float X, float y );// Rectangular coordinates
Float X _, Y _;
};
Inline point: Point (float X, float y)
: X _ (x), Y _ (y ){}
Inline point: rectangular (float X, float y)
{Return point (x, y );}
Inline point: polar (float radius, float angle)
{Return point (radius * Cos (angle), radius * sin (angle ));}
Int main ()
{
Point P1 = point: rectangular (5.7, 1.2 );// Obviously rectangular
Point P2 = point: polar (5.7, 1.2 );// Obviously polar
...
}
If point has a derived class, it is constructed in protected.