C ++ operator +, + =, & lt;, = overload model Column

Source: Internet
Author: User

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.