c++ this

this

成员函数的隐含参数
在C++的类中,每个非静态函数中都有一个this指针,存在于成员函数中,做为函数的第一个形参,可是不会显示,指向当前调用函数
this指针的类型取决于使用this 的成员函数类型以及对象类型,this指针并非成员的一部分,sizeof无用ios

#include<iostream>
using namespace std;
class A
{
public:
    A()
    {
         m =1;
    }
    void Display()
    {
        cout << this << endl;
        cout << (*this).m << endl;
        cout << this->m << endl;
        cout << m << endl;
    }
private:
    int m;
};
int main()
{
    A a;
    A b;
    a.Display();
    b.Display();
}

在这里插入图片描述

若是类里面成员变量不设置private或者public或protect,
struct默认为public,class默认为private
ide

相关文章
相关标签/搜索