Operator overload (converted by me)

Source: Internet
Author: User



it is better to repost it for sharing, but it cannot be reprinted here, so you have to copy it!


const A & operator (Symbol) (parameter) When rewriting Operators) {}< br> If the first parameter is a constant, for example, classa M = 1 + m;
define friend Meta class friend const A & operator (int I, A & M) {};

the caller himself is on the left of the general operator, the class on the right of the operator is the parameter added as

during unit operations, the left ++ and right ++ must be distinguished by an int type parameter.

const A & operator ++ () {// left, and you need to temporarily Add a Class A to think about it as the return value. Because the left ++ does not change the current return value.

}

const A & operator ++ (INT) {// right

}

// Test. cpp: Define the console applicationProgram.
//

# Include "stdafx. H"

Class {
Public:
Explicit A (INT num ){
B = num;
}
A (){
B = 0;
}
Const A & operator ++ (){
B ++;
Return * this;
}
Const A & operator ++ (INT ){
A t (B );
+ + B;
Return T;
}
Void write (const ){
Printf ("Current Value: % d \ n", a. B );
}
Const A & operator = (const A & J ){
If (& J = This) return * this;
B = J. B;
Return * this;
}
Const A & operator = (Int J ){
B = J;
Return * this;
}
Const A & operator + (const A & J) {a t; T. B = B + J. B; return t ;}
Const A & operator + = (const A & J) {This-> B = B + J. B; return * This ;};
Friend const A & operator + (int I, const A & J) {a t; T. B = I + J. B; return t ;}
PRIVATE:
Int B;
};

Int _ tmain (INT argc, _ tchar * argv [])
{
A A (100), B (200 );
// A = Operator + (3, B); A. Write ();
A + = B;
A = 1;
A. Write ();

A. Write (++ );

A. Write (A ++ );

A. Write ();
System ("pause ");
Return 0;
}

I switched to another person:
Http://www.cnblogs.com/25-to-life/archive/2011/10/13/2211033.htm

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.