返回信息流#include <iostream>
using namespace std;
class Point {
public:
Point (int val) { x = val; }
Point operator ++() { x++; return *this; }
Point operator ++(int) { Point old = *this; ++(*this); return old; }
Point operator +(Point a) { x += a.x; cout<<"ad"<<endl;return *this; }
int GetX() const { return x; }
private:
int x;
};
int main()
{
Point a(10);
cout << (++a).GetX();
cout << a++.GetX();
}
请问(++a).GetX()和a++.GetX();是怎么调用的呢,还有Point operator ++(int)中的int有什么用呢
这是一条镜像帖。来源:北邮人论坛 / cpp / #71737同步于 2013/6/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
运算符重载
century
2013/6/13镜像同步9 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
所以这行有问题?
Point operator ++() { x++; return *this; }
应该是 Point& ?
【 在 qq521 的大作中提到: 】
: 形参int 只是为了区分前缀和后缀,有形参表示后缀。编译器编译时候自动把int形参填充为0.
: 另外前缀返回一个引用。
【 在 LafengHu 的大作中提到: 】
: 所以这行有问题?
: Point operator ++() { x++; return *this; }
: 应该是 Point& ?
嗯。对。
C++primer
或者 effective C++
或者 more effective C++
上面都有详细的介绍,你可以看下。
primer已翻完。。
effective在落灰。。
more effective未入手。。
多年不搞c++,全忘光伤心落泪。。
【 在 qq521 的大作中提到: 】
: 嗯。对。
: C++primer
: 或者 effective C++
: ...................
【 在 LafengHu 的大作中提到: 】
: primer已翻完。。
: effective在落灰。。
: more effective未入手。。
: ...................
咱俩可以共同进步呀。我也刚学没多久。