Understanding this pointer in C++

lead out

We first understand this pointer, and we must first understand class. Class is actually equivalent to a structure in C language, and it also creates a custom type. For classes, member functions and member variables can be stored under this type. A class can declare a variable, perform a series of initialization, assignment... operations on the variable, and each time the variable is operated on, the address of the variable will be passed, and the address of the variable will be stored in the this pointer variable. Right now:

The C++ compiler adds a hidden pointer parameter to each "non-static member function", allowing the pointer to point to the current object (the object that calls the function when the function is running). All operations on "member variables" in the function body, They are all accessed through this pointer. It's just that all operations are transparent to the user, that is, the user does not need to pass it, the compiler automatically completes it.

feature

1. The type of this pointer: class type * const, that is, in member functions, this pointer cannot be assigned a value.
2. It can only be used inside the "member function" (cannot appear in the place of formal parameters)
3. The this pointer is essentially the formal parameter of the "member function". When the object calls the member function, the object address is passed as an actual parameter. Give
this formal parameter. Therefore, the this pointer is not stored in the object.
4. The this pointer is the first implicit pointer parameter of the "member function". Generally, it is automatically passed by the compiler through the ecx register
and does not need to be passed by the user.

Example

class A
{
public:
	void Print()
	{
		cout << "Print()" << endl;
	}
private:
	int _a;
};
int main()
{
	A* p = nullptr;
	p->Print();
	return 0;
}

Will the program execute incorrectly? ? ?

In fact, there is no problem, but didn't a null pointer dereference occur here?

In fact, when calling the Print member function, the Print member function is not found in the space pointed by the p pointer. The member functions in the class are actually stored in the public code area, not stored together with the member variables. When calculating the size of a class as shown below, member functions are not actually included in the calculation:

So p->Print(); in the original code does not find this function in the object pointed to by p, but finds this function in the public code area, so no null pointer dereference occurs at all, and this The pointer is stored as null



class A
{
public:
	void PrintA()
	{
		cout << _a << endl;
	}
private:
	int _a;
};
int main()
{
	A* p = nullptr;
	p->PrintA();

	return 0;
}

check it out,? ? ?

This program will actually hang.

	void PrintA()
	{
		cout << _a << endl;//类似于cout << this->_a << endl;
	}

This is actually implicit, and it is displayed like this in the code, and this is null, so a dereference to the null pointer occurs here.

Guess you like

Origin blog.csdn.net/C_Rio/article/details/131980908