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