您的位置:首页 > 其它

单目运算符重载为成员函数 前++ 和后++的使用

2014-06-11 02:34 323 查看
//单目运算符重载为成员函数  后++ 采用了哑元
#include<iostream>
using namespace std;
class CPoint {
private:
int x,y;
public:
CPoint(int a = 0,int b = 0){
x = a;
y = b;
}
CPoint operator++(int);
void display();
};
CPoint CPoint::operator++(int){
x++;
y++;
return *this;
}
void CPoint::display(){
cout << x << " " << y << endl;
}
int main(void){
CPoint p;
p++;
p.display();
return 0;
}

//单目运算符重载为成员函数  前++
#include <iostream>
using namespace std;
class CPoint {
private:
int x,y;
public:
CPoint(int a = 0,int b = 0){
x = a;
y = b;
}
CPoint operator++();
void display();
};
CPoint CPoint::operator++(){
x++;
y++;
return *this;
}
void CPoint::display(){
cout << x << " " << y << endl;
}
int main(void){
CPoint p;
++p;
p.display();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  重载 单目运算符