C++ this指针详解
2015-10-24 17:08
337 查看
http://c.biancheng.net/cpp/biancheng/view/201.html
http://just-study.blogbus.com/logs/25814415.html http://www.cnblogs.com/hnrainll/archive/2011/05/20/2051939.html
this 是C++中的一个关键字,也是一个常量指针,指向当前对象(具体说是当前对象的首地址)。通过 this,可以访问当前对象的成员变量和成员函数。
假设 this 指向 stu 对象,那么下面的语句中,this 就和 pStu 的值相同:
Student stu; //通过Student类来创建对象
Student *pStu = &stu;
[示例] 通过 this 来访问成员变量:
class Student{
private:
char *name;
int age;
float score;
public:
void setname(char *);
void setage(int);
void setscore(float);
};
void Student::setname(char *name){
this->name = name;
}
void Student::setage(int age){
this->age = age;
}
void Student::setscore(float score){
this->score = score;
}
本例中,函数参数和成员变量重名是没有问题的,因为通过 this 访问的是成员变量,而没有 this 的变量是函数内部的局部变量。例如对于
下面是一个完整的例子:
#include <iostream>
using namespace std;
class Student{
private:
char *name;
int age;
float score;
public:
void setname(char *);
void setage(int);
void setscore(float);
void say();
};
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::say(){
cout<<this->name<<"的年龄是 "<<this->age<<",成绩是 "<<this->score<<endl;
}
int main(){
Student stu1;
stu1.setname("小明");
stu1.setage(15);
stu1.setscore(90.5f);
stu1.say();
Student stu2;
stu2.setname("李磊");
stu2.setage(16);
stu2.setscore(80);
stu2.say();
return 0;
}
运行结果:
小明的年龄是 15,成绩是 90.5
李磊的年龄是 16,成绩是 80
对象和普通变量类似;每个对象都占用若干字节的内存,用来保存成员变量的值,不同对象占用的内存互不重叠,所以操作对象A不会影响对象B。
上例中,创建对象 stu1 时,this 指针就指向了 stu1 所在内存的首字节,它的值和 &stu1 是相同的;创建对象 stu2 时,this 等于 &stu2;创建对象 stu3 时也一样。
我们不妨来证明一下,给 Student 类添加一个成员函数,输出 this 的值,如下所示:
void Student::printThis(){
cout<<this<<endl;
}
然后在 main 函数中创建对象并调用 printThis:
Student stu1, *pStu1 = &stu1;
stu1.printThis();
cout<<pStu1<<endl;
Student stu2, *pStu2 = &stu2;
stu2.printThis();
cout<<pStu2<<endl;
运行结果:
0x28ff30
0x28ff30
0x28ff10
0x28ff10
可以发现,this 确实指向了当前对象的首地址,而且对于不同的对象,this 的值也不一样。
几点注意:
this 是常量指针,它的值是不能被修改的,一切企图修改该指针的操作,如赋值、递增、递减等都是不允许的。
this 只能在成员函数内部使用,其他地方没有意义,也是非法的。
只有当对象被创建后 this 才有意义,因此不能在 static 成员函数中使用,后续会讲到。
this 作为隐式参数,本质上是成员函数的局部变量,不占用对象的内存,只有在发生成员函数调用时才会给 this 赋值,函数调用结束后,this 被销毁。
正因为 this 是参数,表示对象首地址,所以只能在函数内部使用,并且对象被实例化以后才有意义。
在《C++函数编译原理和成员函数的实现》一节中讲到,成员函数最终被编译成与对象无关的普通函数,除了成员变量,会丢失所有信息,所以编译时要在成员函数中添加一个额外的参数,把当前对象的首地址传入,以此来关联成员函数和成员变量。这个额外的参数,实际上就是 this,它是成员函数和成员变量关联的桥梁。
http://just-study.blogbus.com/logs/25814415.html http://www.cnblogs.com/hnrainll/archive/2011/05/20/2051939.html
this 是C++中的一个关键字,也是一个常量指针,指向当前对象(具体说是当前对象的首地址)。通过 this,可以访问当前对象的成员变量和成员函数。
所谓当前对象,就是正在使用的对象,例如对于
stu.say();,stu 就是当前对象,系统正在访问 stu 的成员函数 say()。
假设 this 指向 stu 对象,那么下面的语句中,this 就和 pStu 的值相同:
Student stu; //通过Student类来创建对象
Student *pStu = &stu;
[示例] 通过 this 来访问成员变量:
class Student{
private:
char *name;
int age;
float score;
public:
void setname(char *);
void setage(int);
void setscore(float);
};
void Student::setname(char *name){
this->name = name;
}
void Student::setage(int age){
this->age = age;
}
void Student::setscore(float score){
this->score = score;
}
本例中,函数参数和成员变量重名是没有问题的,因为通过 this 访问的是成员变量,而没有 this 的变量是函数内部的局部变量。例如对于
this->name = name;语句,赋值号左边是类的成员变量,右边是 setname 函数的局部变量,也就是参数。
下面是一个完整的例子:
#include <iostream>
using namespace std;
class Student{
private:
char *name;
int age;
float score;
public:
void setname(char *);
void setage(int);
void setscore(float);
void say();
};
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::say(){
cout<<this->name<<"的年龄是 "<<this->age<<",成绩是 "<<this->score<<endl;
}
int main(){
Student stu1;
stu1.setname("小明");
stu1.setage(15);
stu1.setscore(90.5f);
stu1.say();
Student stu2;
stu2.setname("李磊");
stu2.setage(16);
stu2.setscore(80);
stu2.say();
return 0;
}
运行结果:
小明的年龄是 15,成绩是 90.5
李磊的年龄是 16,成绩是 80
对象和普通变量类似;每个对象都占用若干字节的内存,用来保存成员变量的值,不同对象占用的内存互不重叠,所以操作对象A不会影响对象B。
上例中,创建对象 stu1 时,this 指针就指向了 stu1 所在内存的首字节,它的值和 &stu1 是相同的;创建对象 stu2 时,this 等于 &stu2;创建对象 stu3 时也一样。
我们不妨来证明一下,给 Student 类添加一个成员函数,输出 this 的值,如下所示:
void Student::printThis(){
cout<<this<<endl;
}
然后在 main 函数中创建对象并调用 printThis:
Student stu1, *pStu1 = &stu1;
stu1.printThis();
cout<<pStu1<<endl;
Student stu2, *pStu2 = &stu2;
stu2.printThis();
cout<<pStu2<<endl;
运行结果:
0x28ff30
0x28ff30
0x28ff10
0x28ff10
可以发现,this 确实指向了当前对象的首地址,而且对于不同的对象,this 的值也不一样。
几点注意:
this 是常量指针,它的值是不能被修改的,一切企图修改该指针的操作,如赋值、递增、递减等都是不允许的。
this 只能在成员函数内部使用,其他地方没有意义,也是非法的。
只有当对象被创建后 this 才有意义,因此不能在 static 成员函数中使用,后续会讲到。
this 到底是什么
实际上,this 指针是作为函数的参数隐式传递的,它并不出现在参数列表中,调用成员函数时,系统自动获取当前对象的地址,赋值给 this,完成参数的传递,无需用户干预。this 作为隐式参数,本质上是成员函数的局部变量,不占用对象的内存,只有在发生成员函数调用时才会给 this 赋值,函数调用结束后,this 被销毁。
正因为 this 是参数,表示对象首地址,所以只能在函数内部使用,并且对象被实例化以后才有意义。
在《C++函数编译原理和成员函数的实现》一节中讲到,成员函数最终被编译成与对象无关的普通函数,除了成员变量,会丢失所有信息,所以编译时要在成员函数中添加一个额外的参数,把当前对象的首地址传入,以此来关联成员函数和成员变量。这个额外的参数,实际上就是 this,它是成员函数和成员变量关联的桥梁。
相关文章推荐
- 一张图看懂MingW GCC 5.2.0中的C++的的拷贝构造函数
- C#调用C++ dll的两种方法
- 声明和定义
- C语言 判断花括号是否成对出现
- c语言之栈存储区和堆区理解
- C++中const用法总结
- 004--C++11的初始化方式
- C++代码优化方法总结
- 字母大小写转化
- C语言 求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222
- 学籍管理系统 c++作业
- C语言整理-8
- C语言整理-7
- C++中几种将整数转换成二进制输出的方法
- C语言整理-6
- c++指针总结(易混淆)
- c++运算符优先级总结
- C++的头文件和实现文件分别写什么
- C语言整理-5
- 002--C++程序的创建