SoFunction
Updated on 2025-05-09

Introduction to this pointer in C++ and explanation of use examples

Introduction and use of this pointer

The function of pointers

Cat &cmpAge(Cat &other)
{
	if(>age)
	    return other;
	else
	    return *this;
}

Use this example to introduce this pointer:
A pointer to the current object. Which object calls a member function, this pointer points to the object

Example code 1: (this pointer stores the address of the current object)

#include <iostream>
using namespace std;
/*
     Introducing this pointer: A pointer specifically used by C++ to point to the current object address
                   Who is the current object, this pointer will automatically store the address
     Current object: Whoever calls the member function is the current object
 */
class Rect
{
public:
    /*
         Bottom Principle:
           Current object.show();
           Rect *this=&current object
     */
    void show()
    {
        cout<<"The address printed by this pointer is the address of the current object: "<<this<<endl;
    }
};
int main(int argc,char **argv)
{
    //Create an object of the rectangle class    Rect r1;
    Rect r2;
    cout<<"r1's address: "<<&r1<<endl;
    cout<<"R2's address: "<<&r2<<endl;
    //Current object: Who (r1) calls the member function, who (r1) is the current object    ();
    //Current object: Who (r2) calls the member function, who (r2) is the current object    ();
}
/*
 Execution results:
	 The address of r1: 0x7ffdf90a5cb6
	 The address of r2: 0x7ffdf90a5cb7
	 The address printed by this pointer is the address of the current object: 0x7ffdf90a5cb6
	 The address printed by this pointer is the address of the current object: 0x7ffdf90a5cb7
 */

Example Code 2: (Using this pointer)

#include <iostream>
using namespace std;
/*
     Introducing this pointer: A pointer specifically used by C++ to point to the current object address
                   Who is the current object, this pointer will automatically store the address
     Definition method: Compare the sizes of two rectangular objects (compare by w and h, and w and h are required to be larger than another rectangle at the same time), and return the larger object
 */
class Rect
{
public:
    //Define the method to set the value of w and h, and use w and h indirectly    void setAttr(float _w,float _h);
    //Compare the size of two rectangles    Rect compare(Rect &other)
    {
        if((this->w)> && (this->h)>)
        {
            return *this;
        }
        else
            return other;
    }
    void show()
    {
        cout<<"Width: "<<w<<endl;
        cout<<"high: "<<h<<endl;
    }
private:
    //property    float w;  
    float h; 
};
void Rect::setAttr(float _w,float _h)
{
    w=_w;
    h=_h;
}
int main(int argc,char **argv)
{
    //Create an object of the rectangle class    Rect r1;
    Rect r2;
    //Set width and height    (9.8,5.6);
    (1.2,0.5);
    //Compare size    //Writing method 1: The current object is r1    //Rect temp=(r2);
    //();
    //Writing method 2: The current object is r2    Rect temp=(r1);
    ();
}
/*
 Execution results:
     Width: 9.8
     High: 5.6
 */ 

How to write a pointer

this->age //Pointer call
(*this).age //this dereference

Sample code:

#include <iostream>
using namespace std;
/*
     This pointer usually writes member function code, which can be omitted
 */
class Rect
{
public:
    void show()
    {
    }
    void setAttr(float _w,float _h)
    {
        //Writing method 1: Standard writing method -->Writing comprehensive        //this->w=_w;
        //this->h=_h;
        //Writing method 2: Omitting this writing method        w=_w;
        h=_h;
    }
private:
    float w;
    float h;
};
int main(int argc,char **argv)
{
    Rect r1;
    Rect r2;
    (1.2,0.8);
}

This is the article about the introduction and use of this pointer in C++. For more related C++ this pointer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!