zoukankan      html  css  js  c++  java
  • C++ this指针详解

    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,可以访问当前对象的成员变量和成员函数。

    所谓当前对象,就是正在使用的对象,例如对于stu.say();,stu 就是当前对象,系统正在访问 stu 的成员函数 say()。

    假设 this 指向 stu 对象,那么下面的语句中,this 就和 pStu 的值相同:

    1. Student stu; //通过Student类来创建对象
    2. Student *pStu = &stu;


    [示例] 通过 this 来访问成员变量:

    1. class Student{
    2. private:
    3. char *name;
    4. int age;
    5. float score;
    6. public:
    7. void setname(char *);
    8. void setage(int);
    9. void setscore(float);
    10. };
    11. void Student::setname(char *name){
    12. this->name = name;
    13. }
    14. void Student::setage(int age){
    15. this->age = age;
    16. }
    17. void Student::setscore(float score){
    18. this->score = score;
    19. }

    本例中,函数参数和成员变量重名是没有问题的,因为通过 this 访问的是成员变量,而没有 this 的变量是函数内部的局部变量。例如对于this->name = name;语句,赋值号左边是类的成员变量,右边是 setname 函数的局部变量,也就是参数。

    下面是一个完整的例子:

    1. #include <iostream>
    2. using namespace std;
    3. class Student{
    4. private:
    5. char *name;
    6. int age;
    7. float score;
    8. public:
    9. void setname(char *);
    10. void setage(int);
    11. void setscore(float);
    12. void say();
    13. };
    14. void Student::setname(char *name){
    15. this->name = name;
    16. }
    17. void Student::setage(int age){
    18. this->age = age;
    19. }
    20. void Student::setscore(float score){
    21. this->score = score;
    22. }
    23. void Student::say(){
    24. cout<<this->name<<"的年龄是 "<<this->age<<",成绩是 "<<this->score<<endl;
    25. }
    26. int main(){
    27. Student stu1;
    28. stu1.setname("小明");
    29. stu1.setage(15);
    30. stu1.setscore(90.5f);
    31. stu1.say();
    32. Student stu2;
    33. stu2.setname("李磊");
    34. stu2.setage(16);
    35. stu2.setscore(80);
    36. stu2.say();
    37. return 0;
    38. }

    运行结果:
    小明的年龄是 15,成绩是 90.5
    李磊的年龄是 16,成绩是 80

    对象和普通变量类似;每个对象都占用若干字节的内存,用来保存成员变量的值,不同对象占用的内存互不重叠,所以操作对象A不会影响对象B。

    上例中,创建对象 stu1 时,this 指针就指向了 stu1 所在内存的首字节,它的值和 &stu1 是相同的;创建对象 stu2 时,this 等于 &stu2;创建对象 stu3 时也一样。

    我们不妨来证明一下,给 Student 类添加一个成员函数,输出 this 的值,如下所示:

    1. void Student::printThis(){
    2. cout<<this<<endl;
    3. }

    然后在 main 函数中创建对象并调用 printThis:

    1. Student stu1, *pStu1 = &stu1;
    2. stu1.printThis();
    3. cout<<pStu1<<endl;
    4. Student stu2, *pStu2 = &stu2;
    5. stu2.printThis();
    6. cout<<pStu2<<endl;

    运行结果:
    0x28ff30
    0x28ff30
    0x28ff10
    0x28ff10

    可以发现,this 确实指向了当前对象的首地址,而且对于不同的对象,this 的值也不一样。

    几点注意:

    • this 是常量指针,它的值是不能被修改的,一切企图修改该指针的操作,如赋值、递增、递减等都是不允许的。
    • this 只能在成员函数内部使用,其他地方没有意义,也是非法的。
    • 只有当对象被创建后 this 才有意义,因此不能在 static 成员函数中使用,后续会讲到。

    this 到底是什么

    实际上,this 指针是作为函数的参数隐式传递的,它并不出现在参数列表中,调用成员函数时,系统自动获取当前对象的地址,赋值给 this,完成参数的传递,无需用户干预。

    this 作为隐式参数,本质上是成员函数的局部变量,不占用对象的内存,只有在发生成员函数调用时才会给 this 赋值,函数调用结束后,this 被销毁。

    正因为 this 是参数,表示对象首地址,所以只能在函数内部使用,并且对象被实例化以后才有意义。

    在《C++函数编译原理和成员函数的实现》一节中讲到,成员函数最终被编译成与对象无关的普通函数,除了成员变量,会丢失所有信息,所以编译时要在成员函数中添加一个额外的参数,把当前对象的首地址传入,以此来关联成员函数和成员变量。这个额外的参数,实际上就是 this,它是成员函数和成员变量关联的桥梁。

  • 相关阅读:
    JS语言中的JSON.parse()和JSON.stringify()
    Django中 @login_required用法简介
    Django model中的save后的return
    windows下gethostbyname 调用失败
    学习打造自己的DEBUG_NEW
    关于new/delete、malloc/free的内存泄漏检测
    C++连接mysql的两种方式(ADO连接和mysql api连接)
    windows下Redis编译安装
    mysql之字符编码问题
    mysql错误用法insert into where
  • 原文地址:https://www.cnblogs.com/zhizhan/p/4907190.html
Copyright © 2011-2022 走看看