class Student{
public:
Student();
Student(string name, int score);
string getName();
int getScore();
void insert(string name, int score);
friend ostream& operator<<(ostream& os, const Student& student){
os << "Name: "<<student.name<<",Score:"<<student.score<<endl;
return os;
}
private:
string name;
int score;
};
string Student::getName(){
return name;
}
int Student::getScore(){
return score;
}
我定义上课
和主要功能,我定义了一个比较功能
int compStudent(const Student &student1, const Student &student2){
int score1 = student1.getScore();
int score2 = student2.getScore();
if(score1 == score2) return 0;
if(score1<score2) return -1;
return 1;
}
然后错误说这个参数是const但函数不是const。
然后我删除了const,它为什么起作用?
为了能够调用getScore()
一个const
对象,需要声明该方法const
:
class Student{
...
int getScore() const;
...
};
int Student::getScore() const {
return score;
}
在C ++方法声明中最后看到“ const”的含义吗?进行讨论。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句