The first method of invocation:
Demo
#include <iostream>using namespace Std;class text{public:text ()/parameterless constructor {m_a = 0;m_b = 0;cout << "parameterless constructor" & lt;< Endl;} Text (int a)//parametric constructor {m_a = A;m_b = 0;cout << parameterless constructor << Endl;} Text (int A, int b)//parametric constructor, three calling methods {m_a = A;m_b = B;cout << "parametric constructor" << Endl;} The assignment constructor, also called the copy constructor, Text (const text& obj) {cout << "This is also a constructor" << Endl;} ~text (); void Printt () {cout << ordinary member functions << endl;cout << "m_a" << m_a << "M_a" << M_b & lt;< Endl;} Private:int M_a;int m_b;};/ /1 Assignment constructor, with 1 objects to initialize another object int main () {Text T1 (1, 2); Text T0 (1, 2);//assignment = operation, no constructor is called T0 = t1;//is assigned to t0 with T1, and initialization is two different concepts//First Call timing text t2 = t1; Use T1 to initialize T2t2.printt;return 0;}
The second method of invocation:
Demo
The second method of calling int main () {Text T1 (1, 2); Text T0 (1, 2); Text T2 (T1); Initializes the T2 object with the T1 object T2.printt (); return 0;}
The third method of invocation:
Demo
<pre name= "code" class= "CPP" > #include <iostream>using namespace std;class location{public:location (int xx = 0, int yy = 0) {X = XX; Y = Yy;cout << "Constructor object.\n";} Copy constructor, which completes the initialization location of the object (const location & obj) {X = obj. X Y = obj. Y;} ~location () {cout << X << "," << Y << "Object destroyed." << Endl;} int GetX () {return X;} int GetY () {return Y;} Private:int X, y;};/ /business function, formal parameter is an element void F (location p) {cout << p.getx () << Endl;} void Playobj () {Location A (1, 2); Location B = A;cout << "B object has been initialized" << endl;//can set a breakpoint to see the function call jump F (b); The B argument goes to the initial taxiing parameter p, and the copy constructor is called}int Main () {playobj (); return 0;}
The fourth method of invocation:
Demo
#include <iostream>using namespace Std;class location{public:location (int xx = 0, int yy = 0) {X = XX; Y = Yy;cout << "Constructor object.\n";} Copy constructor, which completes the initialization location of the object (const location & obj) {X = obj. X Y = obj. Y;} ~location () {cout << X << "," << Y << "Object destroyed." << Endl;} int GetX () {return X;} int GetY () {return Y;} Private:int X, y;};/ The/g function returns an element//conclusion 1: The return value of the function is an element (complex type), and a new anonymous object is returned (so the copy constructor of the anonymous object class is called)////Conclusion 2: About the Anonymous object's go and stay//If you initialize another object of the same type with an anonymous object, Anonymous object, turn to a famous object//If an anonymous object is assigned to another object of the same type, an anonymous object, the destructor is////so write code, design the compiler's Daniel://Return a new object (No Name anonymous object) location g () {Location A (1, 2); return A;} void Objplay2 () {g ();} void Objplay3 () {//Initialize m with an anonymous object, at this time the C + + compiler directly turns the anonymous pair into m; (righting), from anonymous to a name mlocation m = g ();p rintf ("Anonymous object, be righting, will not be destroyed \ \"); cout << m.getx () << Endl;;} void Objplay4 () {///with an anonymous object assigned to M2, the anonymous object is destroyed by the location m2 (1, 2), M2 = g ();p rintf ("because the anonymous object = To M2, an anonymous object, is refactored \ n"); cout << M2.getx () << Endl;;}
(copy) 4 invocation timing of an assignment constructor or method