新人请教this指针
class ww{
public:
ww(){a=10;}
~ww(){}
const ww& operator++()
private:
int a;
};
const ww& ww::operator++()
{ ++a;
return *this;
}
我是新手 我知道类中隐含this。
这里为什么要使用返回this指针呢?
请大哥大姐指导。
2012-02-04 17:42
ww w; (++w).f();但f()必须是常成员函数(返回类型前面加了const),因为这是前缀++,所以更好的方法是把operator++的返回值修改为ww&,这样就能使用该类的所有的公共成员。

2012-02-04 18:43
2012-02-04 21:02
程序代码:#include <iostream>
using namespace std;
int main()
{
int a = 5;
++(++a); // a -> 7
++(a++); // Error!
return 0;
}
2012-02-04 21:21

2012-02-08 09:15

2012-02-08 14:44