首页 > 其他 > 详细

this

时间:2020-02-05 21:37:27      阅读:75      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using namespace std;

struct Person {
    int m_age;
    
    void run() {    // 函数的代码都在代码区
        cout << "Person::run() - " << m_age << endl; 
    }
};

int main() {        // 函数的代码都在代码区

    Person person1;    //局部变量,栈空间
    person1.m_age = 10;
    person1.run();

    Person person2;    //局部变量,栈空间
    person2.m_age = 20; 
    person2.run();

    getchar();
    return 0;
}
  • 栈空间、堆空间、代码区、全局区

this 存函数调用者的地址,指向了函数调用者

void run() {    //隐式参数,this偷偷给函数传了一个指针地址的参数
    cout << "person::run() - " << this->m_age << endl;    //谁调用run()函数,this就访问谁
}
person1.run();
// this = &person1    this存的是&person1的指针
  • 堆空间只能用指针

this

原文:https://www.cnblogs.com/sec875/p/12266711.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!