C++ 03 —— this

源码

// 03This.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.h"

class Test
{
    int i;
    int j;
public:
    void SetI(int ai)
    {
        cout << "this is :" << this << endl;
        this->i = ai;// 这句话等价于 i = ai;
    }
};

int main(int argc, char* argv[])
{
    Test t1, t2;

    cout << "t1 addr is :" << &t1 << endl;
    t1.SetI(1);
    cout << "t2 addr is :" << &t2 << endl;
    t2.SetI(1);

    //结论:class的member function内部,默认有一个this指针,指向调用者对象
    //问题:所有的member function都含有this指针吗?
    return 0;
}

问题:所有的member function都含有this指针吗?

静态成员函数没有this指针

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章