Contents
  1. I. Member Variables and Member Functions Are Stored Separately
  2. II. The this Pointer
  3. III. Accessing Member Functions Through a Null Pointer
  4. IV. const-Qualified Member Functions

I. Member Variables and Member Functions Are Stored Separately

In C++, a class’s member variables and member functions are stored separately. Only non-static member variables belong to objects of the class.

  1. Empty objects also occupy memory

    The C++ compiler allocates one byte of space to every empty object to mark the object’s location in memory. Each empty object should also have a unique memory address.

    class Person{};
    cout << "size of p = " << sizeof(p) << endl;

    An empty object occupies 1 byte of memory.

  2. Only a class’s non-static member variables belong to objects of the class

    class Person{
    	int m_A;
    };
    cout << "size of p = " << sizeof(p) << endl;

    An object containing only one non-static int member variable occupies 4 bytes of memory.

  3. A class’s static member variables do not belong to objects of the class

    class Person{
    	int m_A;
    	static int m_B;
    };
    int Person::m_B = 100;
    cout << "size of p = " << sizeof(p) << endl;

    The object still occupies 4 bytes of memory.

  4. Member variables and member functions are stored separately

    class Person{
    	int m_A;
    	static int m_B;
    	void func(){}
    };
    cout << "size of p = " << sizeof(p) << endl;

    The object still occupies 4 bytes of memory.

II. The this Pointer

As shown above, member variables and member functions are stored separately in C++.

Each non-static member function has only one function instance. In other words, multiple objects of the same type share one block of code. How, then, does the code distinguish which object invoked it?

This is where the this pointer is used. The this pointer points to the object to which the invoked member function belongs.

The this pointer is an implicit pointer in every non-static member function.

The this pointer does not need to be defined and can be used directly.

Uses of the this pointer:

  • When a parameter and a member variable have the same name, the this pointer can distinguish between them.

  • To return the object itself from a class’s non-static member function, use return *this.

#include<iostream>
using namespace std;

class Person{
public:
	Person(int money){
		//this指针指向被调用的成员函数所属的对象
		this->money = money;	//如果不加this,则赋值两侧会认为是同一个money
	}

	Person& PersonAddMoney(Person &p){
		this->money += p.money;	//将传入的p对象的money加到此对象上
		return *this;	//this是指向对象的指针,*this就是指向对象本体
	}

	int money;
};

void test01(){
	Person p1(18);
	cout << "p1的财产为:" << p1.money << endl;
}

void test02(){
	Person p1(10);
	Person p2(10);
	p2.PersonAddMoney(p1).PersonAddMoney(p1).PersonAddMoney(p1);//链式编程
	cout << "p2的财产为:" << p2.money << endl;

}

int main(){

	//test01();
	test02();

	system("pause");
	return 0;
}

III. Accessing Member Functions Through a Null Pointer

In C++, a null pointer can also call member functions, but you must pay attention to whether the this pointer is used.

If the this pointer is used, add a check to ensure that the code is robust.

#include <iostream>
using namespace std;

class Person{
public:

	void showClassName(){
		cout << "This is Person class." << endl;
	}

	void showPersonAge(){
		if(this == NULL){
			return ;
		}
		cout << "age = " << this->m_Age << endl;	//报错原因是传入的指针为空
	}
	
	int m_Age;
};

void test01(){
	Person * p = NULL;
	p->showClassName();	//空指针也可以正常执行
	p->showPersonAge();	//如果不加if语句会报错
}

int main(){

	test01();

	system("pause");
	return 0;
}

IV. const-Qualified Member Functions

Const member functions:

  • A member function with const appended is called a const member function.

    void showPerson() const {}
  • A const member function cannot modify data members.

    A const member function also has a this pointer, and the this pointer is essentially a const pointer: the pointer cannot be made to point elsewhere.

    Appending this to a member function qualifies what this points to, so the value pointed to by the pointer also cannot be modified.

    class Person{
    public:
    	void showPerson() const {
    		this->m_A = 100;	//报错
    	}
    
    	int m_A;
    }
  • A data member declared with the mutable keyword can still be modified in a const member function.

    mutable int m_B;

    This creates a special variable that can be modified even in a const member function; it can also be modified in a const object.

Const objects

  • Placing const before an object’s type creates a const object.

    const Person p;
  • A const object cannot modify member variables, but variables marked mutable can be modified.

  • A const object can call only const member functions, not ordinary member functions, because ordinary member functions can modify data members.

    Reference: Dark Horse Programmer’s Meticulously Crafted Work | C++ Tutorial: Start at 0 and Advance to 1 with Introductory Programming, Making Programming Easier to Learn Link: https://www.bilibili.com/video/BV1et411b73Z