About C constructor, copy constructor and operator =
1. I don't say the definition of constructive function constructor, I want to say there is only one point, look at the example below: Class C1 {public: // Data member int m_n; // Constructor C1 (INT N = 0) { COUT << "C1 :: C1 () << endl; m_n = n;}}; int _tmain (int Argc, _tchar * argv []) {C1 O (10); // 1. Call constructor C1 O1 = 10; // 2. It can also be compiled, and the constructor is called
C1 O2; // 3 O2 = 10; // 4. Call the constructor, produce temporary object, and then assign the value to O2
RETURN 0;} Tit 1 is easy to understand, directly call the constructor, transmit 10 as the parameter, but the location of the standard 2 is a little solution. Do Int assignment to the C1 object O2? No! See a assignment ( =) In fact, it is initialized, and the constructor is also called (provided in the premise of the appropriate form of constructor, for example, there is only one int parameter, and the 10 on the right side of the = Int); the area of the standard 4 also calls the constructor Put 10 to the constructor, produce a temporary object, then assign this temporary object to O2 (here you want to call Operator = (), if any
However, it seems a little awkward, I declare a C1 object O1, but gives it an INT type initial value. And sometimes "conversion" is dangerous, need to avoid (such as STL, there are many such Example), then you need an Explicit keyword: change the constructor to this: // Constructor Explicit C1 (int N = 0) {cout << "C1: C1 ()" << Endl; m_n = n;}
Then compile the above code, the compiler will report an error. We successfully stop unwelcome conversions. :)
2. The prototype of the copy constructor replication constructor is such that t (const T & SRC) Specifically, the class is // copy constructor C1 (Const C1 & SRC) {cout << "Copy Constructor" << Endl; m_n = src.m_n;} Of course, this class is very simple. It doesn't actually define a replication constructor at all, the system default bitwise copy is enough.
// A function returns the C1 type C1 F1 () {C1 CTEMP (10); // * Return CTEMP;}
INT _TMAIN (int Argc, _tchar * argv []) {c1 n = f1 (); // Here is to call the replication constructor Return 0;} Note: This process calls a constructor (local * place), One copy constructor, if the F1 () is changed to: Return C1 (10);, it can save a call of a replication constructor (equivalent to direct definition C1 N (10)).
3. Operator = This is relatively simple, C1 & Operator = (Const C1 & R) {IF (& R == this) return * this; / / Prevent it from assigning M_N = R.m_n; return * this; // Support continuous assignment} If this member function is defined, the function is called when the following assignment is performed: n2 = n1; // n2, n1 is the C1 type object. Of course, you can also define the Operator = () function of other parameter types. To support objects to C1 to support other types of objects.
It should be noted that n2 = n1; // n2, n1 is all types of C1 types of object calls that operator = rather than assignment constructors! Many beginners (including me) are easily confused.