运算符的重载
1. 友元
2. 内部
#include <iostream>
using namespace std;
class CTmp{
public:
//friend const CTmp operator++(CTmp& tmp);
//friend const CTmp operator++(CTmp& tmp, int);
CTmp(int iflag=0):i(iflag)
{ }
void print()const { cout<<"i:"<<i<<endl; }
const CTmp operator +(const CTmp& info)
{
cout<<"operator+:"<<endl;
return CTmp(i+info.i);
}
const CTmp operator++()
{
cout<<"++CTmp:"<<endl;
i++;
return *this;
}
const CTmp operator++(int)
{
cout<<"CTmp++:"<<endl;
CTmp tmp(i);
this->i++;
return tmp;
}
CTmp& operator +=(const CTmp& info)
{
cout<<"operator+="<<endl;
i += info.i;
return *this;
}
bool operator >=(const CTmp& info)
{
cout<<"operator>=:"<<endl;
return (this->i > info.i)?true:false;
}
private:
int i;
};
/*
const CTmp operator++(CTmp& tmp)
{
cout<<"friend ++tmp:"<<endl;
tmp.i++;
return tmp;
}
const CTmp operator++(CTmp& tmp, int)
{
cout<<"friend tmp++:"<<endl;
CTmp otmp(tmp.i);
tmp.i++;
return otmp;
}
*/
int main()
{
int i = 1, j = 2, k = 3;
CTmp I(i);
CTmp J(j);
CTmp K(k);
K += I+J;
I++;
++I;
if( I >= K )
cout<<"true"<<endl;
else
cout<<"false"<<endl;
return 0;
}