introducing all the constructors
1. Non-parametric construction
2. With parameter construction
3. Copy Construction
4, Mobile Construction (C++11) C + + silently wrote which functions.
Default constructors, copy constructors, assignment operator functions, destructors why not call virtual functions in constructs, destructors.
During the base class partial construction of a derived class object, the type of the object is the base class, not the derived class. Object does not become a derived class until the derived class constructor executes.
Similarly, once a derived class destructor starts executing, the derived class members within the object are undefined values. C + + does not exist, and the object becomes a base class object after it enters the base class destructor.
destructors, which call virtual functions during construction, and such calls do not fall to derived classes . can C + + constructors be virtual functions?
If the constructor is a virtual function, then it needs to be called through vtable , but at this point in the face of a raw memeory, where to find the virtual function table pointers. After all, the virtual function table pointer is initialized in the constructor, not before it. Therefore, the constructor cannot be a virtual function.
Inline is the only legitimate storage class for constructors when the copy constructor is used back. When to write a copy constructor for a class.
Copy constructors are called: When the variable is defined with = (copy initialization): string s = "AA"; Passing an object as a parameter to a unreferenced parameter function returns a unreferenced type Object curly brace initialization array, aggregation class
if destructors are required, copy constructors and assignment operators must be required copy initialization and direct initialization difference:
Direct initialization:
String S2 (S1);
String s2 ("AA");
Direct initialization, using function matching to select the most matching constructor
Copy initialization:
string s = S1;
string s = "AA";
Copy initialization The use of a copy constructor must use several instances of the initialization list. the member variables of the class have a const modifier; (c++11 can also initialize in class) a member of a class is a reference; a member of a class is a class type that has no default constructor, and if the class has an inheritance relationship, the derived class must call the constructor of the base class in its initialization list, and when to declare a virtual destructor.
Declare a virtual destructor for a base class of polymorphic properties:
The inheriting class object is deleted by a base class pointer, the Jocky class is a non-virtual destructor, the component of the inheriting class is not destroyed, which causes the partial destruction object, forms the resource leakage, corrupts the data structure. Construct Order
In virtual derivation, the virtual base class is initialized by the bottommost derived class: The constructor of the lowest derived class controls the initialization of the virtual base class alone.
Objects that contain virtual base classes differ in their general order: First initializes the virtual base class child part of the object with the initial value provided to the lowest derived class constructor , and then initializes it in the order that the direct base class appears in the derived list.
Virtual base classes are always constructed before the Non-virtual base class, regardless of their ordinal position in the inheritance system. what happens when an exception is thrown in a destructor.
When an exception occurs, the destructor is invoked, the destructor throws an exception, and two exceptions exist, the program either ends or causes an ambiguous behavior. The handwriting assignment operator function defines the swap function first Then use the SWAP function in the assignment operator (CAS:COPY-AND-SWAP)
By the way, the SWAP function (effective clause 25): Provides a member swap, and a non-member swap is used to invoke the former.
using namespace Std; class MyClass {public:myclass (int p_):p (p_) {};
void swap (myclass& lhs) {using Std::swap;
Swap (P, LHS.P);
} MyClass & operator = (myclass& lhs) {MyClass tmp (LHS);
Swap (TMP);
return *this;
} Private:int p;
};
void swap (myclass& lhs, myclass& rhs) {lhs.swap (RHS);}
int main () {MyClass A (0), B (1);
Swap (A, b);
b = A;
return 0; }