C ++ constructor & amp; copy constructor & amp; constructor of the derived class & amp; virtual inherited Constructor
Constructor is a special method. It is mainly used to initialize an object when an object is created, that is, assigning an initial value to the object member variable. It is always used together with the new operator in the statement for creating an object. A special class can have multiple constructors, which can be distinguished based on the number of parameters or different parameter types, that is, the overload of constructors. (From Baidu encyclopedia constructor ).
I. Basic Constructors
1 class Base2 {3 public:4 Base(int var) : m_Var(var)5 {6 }7 private:8 int m_Var;9 };
The execution process of the preceding constructor:
1) Pass the parameter 2) open space for the class data member 3) execute the colon syntax to initialize the data member 4) execute the content in the braces of the constructor
It should be noted that the content after the colon syntax is equivalent to int a = 10; (initialization), and the constructor is equivalent to int a; a = 10; (assign initial values)
Ii. copy constructors
1 class Base 2 {3 public: 4 Base (int var): m_Var (var) 5 {6} 7 // copy constructor 8 Base (Base & ref ): m_Var (ref. m_Var) 9 {10} 11 private: 12 int m_Var; 13 };
Why can only parameters of the copy constructor be referenced?
This starts when the copy constructor is triggered. In the following situations, the copy constructor is automatically called:
1) When initializing a new object with an existing object
2) when an object is passed as a value to the form parameter
3) when the function returns an object
Therefore, when an object transmits a function by passing a value, the copy constructor is automatically called to generate an object in the function. If an object is passed in to its own copy constructor, its copy constructor will be called to copy this object so that it can pass in its own copy constructor, this will cause infinite loops until the stack overflows. In addition to being implicitly called when an object is passed into a function, the copy constructor is also called when the object is returned by the function. (From Baidu encyclopedia copy constructor ).
Copy constructor. Generally, you do not need to write it yourself. The default copy constructor of the system can resist this problem. However, in some cases, you need to open up space during the construction. At this time, you need to copy the constructor, the following code is excerpted fromLin RuiDoctor'sHigh-quality C ++ programming guideArticle.
1 class String 2 {3 public: 4 String (const char * str = NULL); // normal constructor 5 String (const String & other); // copy constructor 6 ~ String (void); // destructor 7 private: 8 char * m_data; // used to save String 9}; 10 // String destructor 11 String ::~ String (void) 12 {13 delete [] m_data; 14 // Since m_data is an internal data type, you can also write it as delete m_data; 15} 16 17 // String's normal constructor 18 String: String (const char * str) 19 {20 if (str = NULL) 21 {22 m_data = new char [1]; // 23 * m_data = '\ 0' is better if NULL can be added '; 24} 25 else26 {27 int length = strlen (str); 28 m_data = new char [length + 1]; // 29 strcpy (m_data, str); 30} 31} 32 // copy constructor 33 String: String (const String & other) 34 {35 int length = strlen (other. m_data); 36 m_data = new char [length + 1]; // 37 strcpy (m_data, other. m_data); 38}
Iii. Writing of common derived class Constructor
When defining a derived class object, the construction operation is performed as follows:
1) passing parameters 2) construct the base class based on the declared sequence during inheritance 3) open up space for class data members 4) execute the statement following the colon syntax 5) execute the constructor function body statement
1 class Base 2 {3 public: 4 Base (int B): m_ B (B) 5 {6} 7 private: 8 int m_ B; 9}; 10 11 class Derived: public Base12 {13 public: 14 // write the constructor of a common Derived class 15 Derived (int B, int d): Base (B), m_d (d) 16 {17} 18 private: 19 int m_d; 20 };
Write another example of multi-inheritance:
1 class Base1 2 {3 public: 4 Base1 (int b1): m_b1 (b1) 5 {6} 7 private: 8 int m_b1; 9 }; 10 11 class Base212 {13 public: 14 Base2 (int b2): m_b2 (b2) 15 {16} 17 private: 18 int m_b2; 19}; 20 21 class Derived: public Base1, public Base222 {23 public: 24 Derived (int b1, int b2, int d): Base1 (b1), Base2 (b2), m_d (d) 25 {// note that the order following the colon syntax does not matter. The creation base class is carried out in accordance with the above inheritance Declaration Order... 26} 27 private: 28 int m_d; 29 };
Iv. Writing the constructor of a derived class containing virtual inheritance
Why do we need virtual inheritance?
Virtual inheritance is mainly proposed for the problem of ambiguity in the case of multi-inheritance. For example, the following code requires virtual inheritance. Otherwise, the Base class is not clear when the Derived class inherits.
Perform the following steps for the virtual inheritance constructor:
1) Pass the parameter 2) create a base class. Note that at this time, you need to create all base classes of "constructor with Parameters", including direct base classes and indirect base classes. 3) open up space for class data members. 4) execute the colon syntax. 5) execute the constructor body.
Note: You may wonder if the following code has not created the Base indirect Base class three times? This is not the case. The Compiler does this. When the farthest Derived class Derived creates the Base class, its statements for directly creating the Base class will be ignored.
1 class Base 2 {3 public: 4 Base (int B): m_ B (B) 5 {6} 7 private: 8 int m_ B; 9}; 10 11 class Base1: virtual public Base12 {13 public: 14 Base1 (int B, int b1): Base (B), m_b1 (b1) 15 {16} 17 private: 18 int m_b1; 19 }; 20 21 class Base2: virtual public Base22 {23 public: 24 Base2 (int B, int b2): Base (B), m_b2 (b2) 25 {26} 27 private: 28 int m_b2; 29}; 30 // virtual inheritance, avoiding ambiguity 31 class Derived: public Base1, public base={ 33 public: 34 Derived (int B, int b1, int b2, int d): Base (B), Base1 (B, b1), Base2 (B, b2), m_d (d) 35 {// note that the order following the colon syntax does not matter. The creation base class is carried out in accordance with the above inheritance Declaration Order... 36} 37 private: 38 int m_d; 39 };
V. Virtual destructor
The virtual destructor is generally generated along with polymorphism. The main mode of polymorphism is to use the pointer or reference of the base class to point to or reference the derived class to form polymorphism.
However, there will be a problem. When we analyze the structure, the base class constructor will be called because it is a base class pointer, resulting in derived memory overflow. To solve this problem, the concept of virtual destructor is introduced. Declare the constructor of the base class as virtual, so that it can accurately call the destructor of the derived class when calling the destructor.
The following code must use a virtual destructor to accurately describe the derived class and release the occupied memory.
1 class Base 2 {3 public: 4 Base (int B): m_ B (B) 5 {6} 7 // virtual destructor, so that the base class pointer can accurately release the content in the derived class pointed to 8 virtual ~ Base () 9 {10} 11 private: 12 int m_ B; 13}; 14 15 class Derived: public Base16 {17 public: 18 Derived (int B, char * pStr ): base (B) 19 {20 m_pStr = new char [strlen (pStr) + 1]; 21 strcpy (m_pStr, pStr); 22} 23 ~ Derived () 24 {25 delete m_pStr; 26 m_pStr = NULL; 27} 28 private: 29 char * m_pStr; 30}; 31 32 int main (void) 33 {34 char * pStr = "abcdefg"; 35 Base * B = new Derived (1, pStr); 36 delete B; 37 38 return 0; 39}
Address: http://www.cnblogs.com/nchar/p/3911427.html