Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what scenario 'this' pointer is passed to the class methods? [duplicate]

I was doing some reading on the 'this' pointer, and I think I understand it more than I originally did, but I still need some clarification. So, by my understanding, if you have

class Simple
{
private:
    int m_nID;

public:
    Simple(int nID)
    {
        SetID(nID);
    }

    void SetID(int nID) { m_nID = nID; }
    int GetID() { return m_nID; }
};

The SetID(int nID) function actually is semantically converted into:

void SetID(Simple* const this, int nID) { this->m_nID = nID; }

It makes sense that, there is a this pointer for all member functions of a class, for the most part. But what happens if you have a member function that takes no arguments? Is there a 'this' pointer? If so, does it point to the return type instead of the argument type?

like image 443
UnworthyToast Avatar asked Nov 27 '25 07:11

UnworthyToast


1 Answers

But what happens if you have a member function that takes no arguments? Is there a 'this' pointer? If so, does it point to the return type instead of the argument type?

Even your method do not have any argument, it still have one hidden parameter, that is this pointer.

like image 55
Pranit Kothari Avatar answered Nov 28 '25 20:11

Pranit Kothari