菜鸟问题请教

leboc 2008-10-23 11:10:13
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s) const{num=n;score=s;}
void display() const {cout<<num<<" "<<score<<endl;}
private:
mutable int num;
mutable float score;
};

int main()
{const Student stud(101,78.5);
void fun(Student &);
fun(stud); //这里,不允许const类型的stud 转换成&stu;
return 0;
}
void const fun(Student &stu)
{
stu.display();
stu.change(101,80.5);
stu.display();
}


请问除了去掉const外,还有什么办法可以用fun?
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
boys2002 2008-10-24
  • 打赏
  • 举报
回复
楼主 冰雪聪明



顶一下 借分
ysuliu 2008-10-24
  • 打赏
  • 举报
回复
解决了已经~~还是自己解决的,不错~
chlaws 2008-10-23
  • 打赏
  • 举报
回复
呵呵,自己解决了印象深些
leboc 2008-10-23
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s) const{num=n;score=s;}
void display() const {cout < <num < <" " < <score < <endl;}
private:
mutable int num;
mutable float score;
};

int main()
{const Student stud(101,78.5);
void fun(Student &); //这里的申明改成void fun(const Student &);
fun(stud); //这里,不允许const类型的stud 转换成&stu;
return 0;
}
void const fun(Student &stu) //这里也改成void fun(const Student &stu);
{
stu.display();
stu.change(101,80.5);
stu.display();
}
就OK了.


我晕.每次都是刚提问,就想到答案了.

为什么分数不能给自己.....

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧