A single operator is used to return a reference. It cannot be a friend function. Otherwise, the compilation fails.
Double operator, which must be defined as youyuan; otherwise, compilation fails.
Test Compiler: g ++ 4.6.3
[Cpp]
# Include <iostream>
# Include <ostream>
# Include <string>
Using namespace std;
Class T {
Public:
T (){};
T (int t)
{
A = t;
};
T (const T & t) // when no replication function is defined, the compiler will synthesize one by itself and will not call the following "=" operator.
{
Cout <"copy" <endl;
A = t.;
};
T & operator = (const T & t) // when no value assignment function is defined, the compiler will synthesize it and will not call the following copy () operator.
{
Cout <"assign value" <endl;
A = t.;
Return * this;
}
T & operator + = (int B)
{
A + = B;
Return * this;
}
Private: // The youyuan function can be a private class. Because youyuan is not a class member, the private restriction does not work for him.
Friend ostream & operator <(ostream & out, const T & t) // must be set as a friend function; otherwise, compilation fails.
{
Out <"the num is:" <t.;
Return out;
}
Friend T operator + (const T & t1, const T & t2) // must be set as a friend function; otherwise, compilation fails.
{
T t;
T. a = t1.a + t2.a;
Return t;
}
Int;
};
Void fun (T t)
{
Return;
}
Int main ()
{
T t (2 );
Fun (t); // The parameter is passed as a copy function.
T t1;
T1 = t; // value assignment operator
T1 + = 5;
Cout <t1 <endl;
T t2; // if it is t2 = t + t1; initialized as a replication function, equivalent to t2 (t + t1)
T2 = t + t1; // Add first, in the call =
Cout <t2 <endl;
Return 0;
}
# Include <iostream>
# Include <ostream>
# Include <string>
Using namespace std;
Class T {
Public:
T (){};
T (int t)
{
A = t;
};
T (const T & t) // when no replication function is defined, the compiler will synthesize one by itself and will not call the following "=" operator.
{
Cout <"copy" <endl;
A = t.;
};
T & operator = (const T & t) // when no value assignment function is defined, the compiler will synthesize it and will not call the following copy () operator.
{
Cout <"assign value" <endl;
A = t.;
Return * this;
}
T & operator + = (int B)
{
A + = B;
Return * this;
}
Private: // The youyuan function can be a private class. Because youyuan is not a class member, the private restriction does not work for him.
Friend ostream & operator <(ostream & out, const T & t) // must be set as a friend function; otherwise, compilation fails.
{
Out <"the num is:" <t.;
Return out;
}
Friend T operator + (const T & t1, const T & t2) // must be set as a friend function; otherwise, compilation fails.
{
T t;
T. a = t1.a + t2.a;
Return t;
}
Int;
};
Void fun (T t)
{
Return;
}
Int main ()
{
T t (2 );
Fun (t); // The parameter is passed as a copy function.
T t1;
T1 = t; // value assignment operator
T1 + = 5;
Cout <t1 <endl;
T t2; // if it is t2 = t + t1; initialized as a replication function, equivalent to t2 (t + t1)
T2 = t + t1; // Add first, in the call =
Cout <t2 <endl;
Return 0;
}
Output result:
Copy
Assignment
The num is: 7
Assignment
The num is: 9