首页 > 编程语言 > 详细

c++ this

时间:2021-03-11 22:20:03      阅读:32      评论:0      收藏:0      [点我收藏+]
this

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

#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

c++ this

原文:https://blog.51cto.com/14982125/2656421

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