帮我看看这段c++
程序代码:
typedef unsigned short USHORT; #include <iostream.h> class Counter { public: Counter(); ~Counter(){} USHORT GetItsVal()const { return itsVal; } void SetItsVal(USHORT x) {itsVal = x; } void Increment() { ++itsVal; } const Counter& operator++ (); private: USHORT itsVal; }; Counter::Counter(): itsVal(0) {}; const Counter& Counter::operator++() { ++itsVal; cout<<this<<endl; return *this; } int main() { Counter i; cout << "The value of i is " << i.GetItsVal() << endl; i.Increment(); cout << "The value of i is " << i.GetItsVal() << endl; ++i; cout<<&i<<endl; cout << "The value of i is " << i.GetItsVal() << endl; Counter a=++i; cout<<&a<<endl; cout<<&i<<endl; a.SetItsVal(8); cout << "The value of a: " << a.GetItsVal(); cout << " and i: " << i.GetItsVal() << endl; return 0; }
我的疑问是const Counter& Counter::operator++() Counter a=++i;返回的应该是1个常量counter对象=a,但为什么后面可以用a.SetItsVal(8);修改a自身的成员变量,还有 return *this;应该是返回i对象,但为什么a和i的内存地址不一样呢,谢谢