C++重载自增运算符(很容易搞混)

#include <iostream.h>

class CPoint

{

private:

int x,y;

public:

CPoint(int xx=0, int yy=0)

{

x = xx;

y = yy;

}

void disp()

{

cout<<"("<<x<<','<<y<<")"<<endl;

}

CPoint operator + (CPoint q)

{

return CPoint(x+q.x,y+q.y);

}

CPoint operator - (CPoint q)

{

return CPoint(x-q.x,y-q.y);

}

CPoint operator ++()  //++CPoint

{

x++;

y++;

cout<<"++() called"<<endl;

return *this;

}

CPoint operator ++(int) //CPoint++

{

CPoint t(x,y);

x++;

y++;

cout<<"++(int) called"<<endl;

return t;

}

};

 

void main() 

{

CPoint a(10,10), b;

a.disp();

b = a++; // call ++(int)

a.disp();

b.disp();

b = ++a; // call ++()

a.disp();

b.disp();

}

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

原文链接: https://www.cnblogs.com/aquar/archive/2011/01/19/3451404.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++重载自增运算符(很容易搞混)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/20267

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午9:43
下一篇 2023年2月7日 下午9:44

相关推荐