
急急急!!!C++程序、求高人指点迷津啊~~~~
#include<iostream>usingnamespacestd;classConstFun{public:voidConstFun(){}constintf5()...
# include <iostream>
using namespace std;
class ConstFun{
public:
void ConstFun(){}
const int f5() const {return 5;}
int Obj() {return 45;}
int val;
int f8();};
int ConstFun::f8(){return val;}
int main(){
const ConstFun s;
int i=s.f5();
cout<<"Value="<<endl;
return o;
}
我什么要把
const int f5() const {return 5;}
中的const 去掉啊????
补充一下 哈 :
这是一道改错题 答案说 需要去掉 const 为什么啊 ? 求高人 指点啊~~ 展开
using namespace std;
class ConstFun{
public:
void ConstFun(){}
const int f5() const {return 5;}
int Obj() {return 45;}
int val;
int f8();};
int ConstFun::f8(){return val;}
int main(){
const ConstFun s;
int i=s.f5();
cout<<"Value="<<endl;
return o;
}
我什么要把
const int f5() const {return 5;}
中的const 去掉啊????
补充一下 哈 :
这是一道改错题 答案说 需要去掉 const 为什么啊 ? 求高人 指点啊~~ 展开
展开全部
const int f5() const {return 5;} 两个const都可以用,这句没有错误。
错误的是void ConstFun(){} 这一句 ,应该把const去掉, 构造函数一定不能有返回值。
当然你如果执意选择相信你的答案就当我没说。
错误的是void ConstFun(){} 这一句 ,应该把const去掉, 构造函数一定不能有返回值。
当然你如果执意选择相信你的答案就当我没说。
追问
ooo 知道了 可能答案错了 呵呵 谢谢哈~~
展开全部
没明白什么意思 ,不知道你在说什么
追问
这是一个改错题 答案说 去掉const 为什么啊?
追答
const修饰非引用类型或者非指针类型时,实际跟没有修饰是一样的,编译器会自动去除,比如以下的描述编译器会认为是重定义
int f5(const int a) const {return 5;}
int f5(int a) const {return 5;}
const int f5(const int a) const {return 5;}这个函数中的前面一个const如我上面提到的
后面一个const是用来修饰这个成员函数内部无法修改成员变量,除非成员变量有特殊申明
这个两个const对于目前这个函数其实没有起到任何作用,可有可无
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询