| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 801 人关注过本帖
标题:帮我看看这段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
快速回复:帮我看看这段c++
数据加载中...
 
   



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

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