标题:小弟新手请教一个问题
取消只看楼主
dive137
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-23
 问题点数:0 回复次数:1 
小弟新手请教一个问题

希望达人赐教一下
class Cat
{
public:
Cat();
Cat(const Cat&);
~Cat();
int const GetAge()
{
return *itsAge;
}

int const GetWeight()
{
return *itsWeight;
}

void SetAge(int age)
{
*itsAge=age;
}

private:
int *itsAge;
int *itsWeight;
};

Cat::Cat()
{
itsAge=new int;
itsWeight=new int;
*itsAge=5;
*itsWeight=3;
}

Cat::Cat(const Cat & rhs)
{
itsAge=new int;
itsWeight=new int;
*itsAge=rhs.GetAge();
*itsWeight=*(rhs.itsWeight);
}


Cat::~Cat()
{
delete itsAge;
itsAge=0;
delete itsWeight;
itsWeight=0;
}

void main()
{
Cat cat;
cout<<cat.GetAge()<<endl;
cat.SetAge(89);
cout<<cat.GetAge()<<endl;
// cat boot(cat);
// cout<<boot.GetAge()<<endl;
cat.SetAge(98);
// cout<<cat.GetAge()<<endl;
// cout<<boot.GetAge()<<endl;
}
vc++6.0编译
:\Program Files\Microsoft Visual Studio\MyProjects\textOverLoad\overLoad.cpp(85) : error C2662: 'GetAge' : cannot convert 'this' pointer from 'const class Cat' to 'class Cat &'
Conversion loses qualifiers

[此贴子已经被作者于2007-7-22 22:31:43编辑过]

搜索更多相关主题的帖子: private public return 
2007-07-22 22:31
dive137
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-23
得分:0 
谢谢了
现在明白了 是没有搞清楚const的含意
2007-07-23 21:55



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




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

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