第 13.3 節

Object Model and the `this` Pointer

0瀏覽次數0訪問次數--跳出率--平均停留

C++ object model and this pointer

Member variables and member functions are stored separately.

In C++, member variables and member functions within a class are stored separately.

Only non-static member variables belong to objects of the class.

class Person {
public:
    Person() {
        mA = 0;
    }
    //非静态成员变量占对象空间
    int mA;
    //静态成员变量不占对象空间
    static int mB; 
    //函数也不占对象空间,所有函数共享一个函数实例
    void func() {
        cout << "mA:" << this->mA << endl;
    }
    //静态成员函数也不占对象空间
    static void sfunc() {
    }
};

int main() {

    cout << sizeof(Person) << endl;


    return 0;
}

Running/Observing the results: After running, the example will print variable values or addresses; address values depend on the runtime environment, so focus on observing the relative positioning and pointer changes of similar objects.

the "this" pointer concept

From 4.3.1, we know that member variables and member functions are stored separately in C++.

Each non-static member function is instantiated only once, meaning multiple objects of the same type share a single block of code.

So the question is: how does this block of code distinguish which object called it?

C++ uses a special object pointer, the this pointer, to solve this problem. The this pointer points to the object to which the called member function belongs

this pointer是隐含在每个非静态成员函数内的一种指针。它指向调用该成员函数的对象本身。在成员函数内部,this指针可以用来访问对象的成员变量和成员函数,尤其在需要区分成员变量和函数参数同名的情况下非常有用。例如,当成员变量和函数参数同名时,可以使用this->成员变量来明确指定是访问成员变量。此外,this指针也常用于链式调用(如返回对象自身的引用)或在函数中传递对象地址给其他函数。值得注意的是,this指针是常量指针(对于非const成员函数)或常量常量指针(对于const成员函数),因此在函数内部不能修改this指针本身的值。

No need to define the this pointer, it can be used directly.

Uses of the this pointer:

  • When parameters have the same name as member variables, you can use the this pointer to distinguish them.
  • In a non-static member function of a class, to return the object itself, you can use return *this.
class Person
{
public:

    Person(int age)
    {
        //1、当形参和成员变量同名时,可用this指针来区分
        this->age = age;
    }

    Person& PersonAddPerson(Person p)
    {
        this->age += p.age;
        //返回对象本身
        return *this;
    }

    int age;
};

void test01()
{
    Person p1(10);
    cout << "p1.age = " << p1.age << endl;

    Person p2(10);
    p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
    cout << "p2.age = " << p2.age << endl;
}

int main() {

    test01();


    return 0;
}

Running/Observing the results: After running, the example will print variable values or addresses; address values depend on the runtime environment, so focus on observing the relative positioning and pointer changes of similar objects.

Accessing a member function via a null pointer

In C++, calling member functions on a null pointer is technically allowed, but it's critical to ensure the function does not access the object through the this pointer. If the member function attempts to use this (for example, to access or modify member variables), it will result in undefined behavior.

If the this pointer is used, it should be checked to ensure code robustness.

Example:

//空指针访问成员函数
class Person {
public:

    void ShowClassName() {
        cout << "我是Person类!" << endl;
    }

    void ShowPerson() {
        if (this == NULL) {
            return;
        }
        cout << mAge << endl;
    }

public:
    int mAge;
};

void test01()
{
    Person * p = NULL;
    p->ShowClassName(); //空指针,可以调用成员函数
    p->ShowPerson();  //但是如果成员函数中用到了this指针,就不可以了
}

int main() {

    test01();


    return 0;
}

Running/Observing the results: After running, the example will print variable values or addresses; address values depend on the runtime environment, so focus on observing the relative positioning and pointer changes of similar objects.

const-qualified member function

Constant function:

  • When we add const after a member function, we call this function a constant function
  • You cannot modify member properties within constant functions.
  • When declaring a member variable with the keyword mutable, it can still be modified in const member functions.

constant object:

  • Adding const before declaring an object makes that object a constant object.
  • A const object can only call const member functions.

Example:

class Person {
public:
    Person() {
        m_A = 0;
        m_B = 0;
    }

    //this指针的本质是一个指针常量,指针的指向不可修改
    //如果想让指针指向的值也不可以修改,需要声明常函数
    void ShowPerson() const {
        //const Type* const pointer;
        //this = NULL; //不能修改指针的指向 Person* const this;
        //this->mA = 100; //但是this指针指向的对象的数据是可以修改的

        //const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
        this->m_B = 100;
    }

    void MyFunc() const {
        //mA = 10000;
    }

public:
    int m_A;
    mutable int m_B; //可修改 可变的
};

//const修饰对象  常对象
void test01() {

    const Person person; //常量对象  
    cout << person.m_A << endl;
    //person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
    person.m_B = 100; //但是常对象可以修改mutable修饰成员变量

    //常对象访问成员函数
    person.MyFunc(); //常对象不能调用const的函数

}

int main() {

    test01();


    return 0;
}

Running/Observing the results: After running, the example will print variable values or addresses; address values depend on the runtime environment, so focus on observing the relative positioning and pointer changes of similar objects.

音乐页