| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 729 人关注过本帖
标题:运算符重载的问题,求助
取消只看楼主 加入收藏
shenyulv
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:166
专家分:150
注 册:2009-3-8
结帖率:96.43%
收藏
已结贴  问题点数:20 回复次数:1 
运算符重载的问题,求助
#include "iostream"
using namespace std;
class Counter
{
public:
    Counter(){value=0;}
    Counter(int i){value=i;}
    Counter operator++();
    Counter operator++(int);
    Counter operator--();
    Counter operator--(int);
    void display() {cout<<value<<endl;}
private:
    unsigned value;
 };
Counter Counter::operator++()
{
    value++;
    return*this; //这个是什么意思?*this 是指哪个?
}
Counter Counter::operator++(int)
{
    Counter temp;
    temp.value=value++;
    return temp;
}
Counter Counter::operator--()
{
    value--;
    return*this;
}
Counter Counter::operator--(int)
{
    Counter temp;
    temp.value=value--;
    return temp;
}
int main()
{
    Counter n(10),c;
    c=++n;
    cout<<"前缀++运算符计算结果:"<<endl;
    cout<<"n=",n.display();
    cout<<"c=",c.display();
    c=n++;
    cout<<"后缀++运算符计算结果:"<<endl;
    cout<<"n=",n.display();
    cout<<"c=",c.display();
    c=--n;
    cout<<"前缀--运算符计算结果:"<<endl;
    cout<<"n=",n.display();
    cout<<"c=",c.display();
    c=n--;
    cout<<"后缀--运算符计算结果:"<<endl;
    cout<<"n=",n.display();
    cout<<"c=",c.display();
return 0;
}


搜索更多相关主题的帖子: 重载 运算符 
2009-11-12 21:51
shenyulv
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:166
专家分:150
注 册:2009-3-8
收藏
得分:0 
吼吼,谢谢咯

叶的离去,是风的过错,还是树的不挽留?
2009-11-13 11:58
快速回复:运算符重载的问题,求助
数据加载中...
 
   



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

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