| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 474 人关注过本帖
标题:输出的意外结果
只看楼主 加入收藏
flyingzc
Rank: 2
等 级:论坛游民
帖 子:22
专家分:13
注 册:2010-4-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
输出的意外结果
#include<iostream>
using namespace std;
class a
{
public:
    a();
    a(int);
    a(const a&);
    ~a(){}
    void SetRadius(int);
    int GetRadius()const{return *itsRadius;}
    const a&operator++();
    const a operator++(int);
    a&operator=(const a&);
private:
    int *itsRadius;
};
a::a()
{
    itsRadius=new int(6);
}
a::a(int radius)
{
    itsRadius=new int(radius);
}
a::a(const a&rsh)
{
    int val=rsh.GetRadius();
    itsRadius=new int(val);
}
a & a::operator =(const a&rsh)
{
    if(this==&rsh)
        return *this;
    *itsRadius=rsh.GetRadius();
    return *this;
}
const a &a::operator ++()
{
    ++(itsRadius);
    return *this;
}
const a a::operator ++(int)
{
    a temp(*this);
    ++(itsRadius);
    return temp;
}
int main()
{
    a one,two(8);
    one++;
    ++two;
    cout<<"one"<<one.GetRadius()<<endl;
    cout<<"two"<<two.GetRadius()<<endl;
    one=two;
    cout<<"one"<<one.GetRadius()<<endl;
    cout<<"two"<<two.GetRadius()<<endl;
    return 0;
}
搜索更多相关主题的帖子: 输出 结果 意外 
2010-04-01 13:12
yyblackyy
Rank: 6Rank: 6
等 级:侠之大者
帖 子:98
专家分:457
注 册:2010-3-31
收藏
得分:20 
const a &a::operator ++()
{
    ++(itsRadius);//改成 ++(*itsRadius);
    return *this;
}
const a a::operator ++(int)
{
    a temp(*this);
    ++(itsRadius);  );//改成 ++(*itsRadius);

    return temp;
}
2010-04-01 13:37
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1481
专家分:9055
注 册:2010-3-16
收藏
得分:0 
楼上说得对
 ++(itsRadius)是对地址进行++,结果是指向下一个存储空间。
 ++(*itsRadius) 是对该地址的内容进行++
2010-04-01 14:12
flyingzc
Rank: 2
等 级:论坛游民
帖 子:22
专家分:13
注 册:2010-4-1
收藏
得分:0 
非常感谢
2010-04-01 16:03
succubus
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:635
专家分:1080
注 册:2007-10-7
收藏
得分:0 
一般看到输出意外的结果了
就找找代码里有没有对非法地址的操作

[url=http:///view/aDU1]/image/aDU1.gif" border="0" />[/url]
2010-04-01 16:46
快速回复:输出的意外结果
数据加载中...
 
   



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

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