标题:帮我看看这段c++
取消只看楼主
luolinpb
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-24
 问题点数:0 回复次数:0 
帮我看看这段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的内存地址不一样呢,谢谢
搜索更多相关主题的帖子: Counter itsVal USHORT operator const 
2008-06-24 09:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-220835-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.199415 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved