T t (2), T t = 2, T = t (2) are completely equivalent.
[Cpp]
# Include <iostream>
# Include <ostream>
# Include <string>
Using namespace std;
Class T {
Public:
T (){};
T (int t)
{
Cout <"copy" <endl;
A = t;
};
T & operator = (const T & t)
{
Cout <"assign value" <endl;
A = t.;
Return * this;
}
Int;
};
Int main ()
{
T t (2 );
Cout <"------------------" <endl;
T t1 = 2;
Cout <"------------------" <endl;
T t2 = T (2 );
Cout <"------------------" <endl;
T t3;
T3 = t2;
Return 0;
}
# Include <iostream>
# Include <ostream>
# Include <string>
Using namespace std;
Class T {
Public:
T (){};
T (int t)
{
Cout <"copy" <endl;
A = t;
};
T & operator = (const T & t)
{
Cout <"assign value" <endl;
A = t.;
Return * this;
}
Int;
};
Int main ()
{
T t (2 );
Cout <"------------------" <endl;
T t1 = 2;
Cout <"------------------" <endl;
T t2 = T (2 );
Cout <"------------------" <endl;
T t3;
T3 = t2;
Return 0;
}
Output result:
Copy
------------------
Copy
------------------
Copy
------------------
Assignment