指向常对象的指针变量和指向对象的常指针
1,指向常对象的指针变量
指向常对象的指针变量可以指向一个已经申明为常对象的变量,此时只能用指向常对象的指针变量指向它;也可以指向一个非常对象的变量,而此时可通过指针访问该对象,但是不能通过指针改变该对象的值。下面给出一个简单程序:
#include <iostream>
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(29,90.5);
Student stud1(30,60.8);