#include
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n;score=s;}
void display(){cout<<num<<" "<<score<<endl;}
private:
int num;
float score;
};
int main()
{
const student stud(101,78.5);
stud.display();
stud.change(101,85.5);
stud.display();
return 0;
#include "stdafx.h"
#include
//#include
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n;score=s;}
void display(){cout<<num<<" "<<score<<endl;}
private:
int num;
float score;
};
int main()
{
** student stud(101,78.5);**
stud.display();
stud.change(101,85.5);
stud.display();
return 0;
}
#include "stdafx.h"
#include
//#include
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n;score=s;}
void display(){cout<<num<<" "<<score<<endl;}
private:
int num;
float score;
};
int main()
{
** student stud(101,78.5);**
stud.display();
stud.change(101,85.5);
stud.display();
return 0;
}
你都定义了const,还怎么修改。
这是能通过编译的代码:必须去掉change中修改的代码
#include <iostream>
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n,float s)const{}
void display() const{cout<<num<<" "<<score<<endl;}
private:
int num;
float score;
};
int main()
{
const student stud(101,78.5);
stud.display();
stud.change(101,85.5);
stud.display();
return 0;
}
#include
using namespace std;
class student
{
p__ublic:
student(int n,float s):num(n),score(s){}
void change(int n,float s)const{}
void display() const{cout<<num<<" "<<score<<endl;}__
private:
int num;
float score;
};
int main()
{
const student stud(101,78.5);
stud.display();
stud.change(101,85.5);
stud.display();
return 0;
}**__
1, #include 这里要完整,如#include
2. main函数中,const student stud(101,78.5)要去掉const. const表示常量,他的值初始化后不允许改变。
修改不了,定义有错误。
#include
using namespace std;
class student
{
public:
student(int n,float s):num(n),score(s){}
void change(int n, float s) {num = n; score = s;}
void display() const {cout<<num<<" "<<score<<endl;}
private:
int num;
float score;
};
int main() {
const student stud(101, 78.5);
student *p = const_cast(&stud);
p->display();
p->change(101,85.5);
p->display();
return 0;
}
使用黑魔法可以的。
具体来说就是把main()
函数改成这样:
int main()
{
const student stud(101,78.5);
student * p = (const_cast<student*>(&stud));
p->display();
p->change(101,85.5);
p->display();
return 0;
}
使用const_cast
可以强制转换变量的const
属性。但是使用后果自负。
将num和score的声明前加上mutable关键字就可以了,这说可以对num和score进行修改。
在C++中,mutable也是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中。
代码可以参照下面的,我已经运行通过了。
#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(int argc, char *argv[]) {
const Student stud(101, 78.5);
stud.display();
stud.change(101, 85.5);
stud.display();
return 0;
}