您的位置:首页 > 编程语言 > C语言/C++

c++ 重载运算符

2015-06-12 16:36 239 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: