this 指针
this是C++中一个关键字,也是const指针。它指向当前对象,通过它可以访问当前对象的所以成员
所谓当前对象,是指正在使用的对象,例如:stu.show(); , stu就是当前对象,this就指向stu。
到底什么是this指针:
this实际上是成员函数的一个形参,在调用成员函数时将对象的地址作为实参传递给this,不过this这个形式是隐式的,它并不出现在代码中,而是在编译阶段由编译器默默将它添加到参数列表中
this作为隐式形参,本质上是成员函数的局部变量,所以只能用在成员函数内部,并且只有在通过对象调用成员函数时才给 this 赋值。
每个成员函数(包括构造和析构)都有一个this指针
this指针指向调用对象,即可以通过this关键字访问当前对象的成员
注意:
this指针的类型为类类型*const (类名* const),为右值
this指针本身不占用大小,他并不是对象的一部分,因此不会影响sizeof的效果
this的作用域在类成员内部
this 指针式类成员函数的第一个默认隐含函数,编译器自动维护传递,类编写者不能显示传递
只有在类的非静态成员函数在才可以使用this指针,其他函数都不可以。
this->成员名; //访问成员变量
this->函数名;//访问成员函数
#include <iostream>
using namespace std;
class Student{
public:
void setname(char *name);
void setage(int age);
void setscore(float score);
void show();
private:
char *name;
int age;
float score;
};
void Student::setname(char *name){
this->name = name;
}
void Student::setage(int age){
this->age = age;
}
void Student::setscore(float score){
this->score = score;
}
void Student::show(){
cout<<this->name<<"的年龄是"<<this->age<<",成绩是"<<this->score<<endl;
}
int main(){
Student *pstu = new Student;
pstu -> setname("李华");
pstu -> setage(16);
pstu -> setscore(96.5);
pstu -> show();
return 0;
}
this只能用在类的内部,通过this可以访问类的所以成员,包括private、protected、public属性的
例子中成员函数中的参数和成员变量重名,只能通过this区分。
this->name=name;//左边的name就是成员变量 右边的name是成员函数中的形参
this虽然作用在类的内部,但是只有在对象被创建以后才会给this赋值,并且赋值过程是编译器完成的,不需要用户干预,用户也不能显式的给this赋值
注意:
class Student
{
public:
//构造函数的重载与普通函数重载相同
Student(); //默认构造
Student(string name,string desc);//带参构造
~Student();
void showInfo();
string GetName()
{
return m_name;
}
void SetName(string val)
{
m_name=val;
}
string Getdesc()
{
return m_desc;
}
int Getag()
{
return m_age;
}
void Setage(int val)
{
if(val<0)
m_age=18;
else
m_age=val;
}
void InitScores();//初始化学生成绩数组,默认分配一个元素空间
void AddScores();//添加一个成绩
private:
string m_Name;
string m_desc;
int m_age;
float* scores;//学生的分数数组
int scorescount;//学生的成绩个数
}
Student::Student()
{
cout<<"默认构造"<<endl;
}
Student::Student(string name, string desc)
{
m_Name=name; //等价写法 SetName(name);
m_desc=desc;
cout<<"带参构造"<<endl;
}
//等价于
Student::Student(string name, string desc):m_Name(name),m_desc(desc) //初始化参数列表
{
InitScores();
}
void Student::ShowInfo()
{
cout<<m_desc<<endl;
}
void Student::InitScores()
{
this->scores=new float[1];
this->scoresCount=1;
}
void Student::AddScores()
{
this->scores[this->scoreCount-1]=score;
//1、创建一个新数组,分配scoreCount+1个空间
//2、复制原数组中的内容到新数组中
//3、scoreCount++
//4、scores指向新数组
float* newScore=new float[scoreCount+1];
float* oldScore=score;
momcpy(newScores,score,sizeof(float)*scoreCount);
scoreCount++;
scores=newScores;
delete oldScores;
}
Student::~Student()
{
delete this->scores;
}
Student stu1; //在栈内存中直接分配空间
Student stu2("马化腾","普通家庭");
stu2.showinfo();
Student* stu=new Student("撒贝宁","北大还行"); //在堆内存中分配空间
stu->ShowInfo();
delect stu;
Student* ptrStu1=new Student("迪丽热巴","微胖女孩");
ptrStu1->AddScore(78.9f);
ptrStu1->AddScore(38.7f);
ptrStu1->AddScore(76.8f);
ptrStu1->AddScore(795.6f);
ptrStu1->AddScore(88.59f);
delete ptrStu1;
作用:
可以用来返回当前对象的引用
class Student{
public:
Strudent& addScore(int score);
}