这个this代表什么
这是网上一篇文章中的程序,运行正常。有一个语句看不懂,特来请教。this->score = score;
这个this代表什么,整个程序中就只有一个,从哪里来的?
原文《深入解析C++中派生类的构造函数》 http://www.
程序如下:
#include<iostream>
using namespace std;
//基类
class People
{
protected:
char *name;
int age;
public:
People(char*, int);//基类构造函数
};
People::People(char *name, int age): name(name), age(age){}
//派生类
class Student: public People
{
private:
float score;
public:
Student(char*, int, float);
void display();
};
//调用了基类的构造函数
Student::Student(char *name, int age, float score): People(name, age)
{
this->score = score;
}
void Student::display()
{
cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}
int main()
{
Student stu("小明", 16, 90.5);
stu.display();
return 0;
}