3

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?

iammilind
  • 62,239
  • 27
  • 150
  • 297
UnworthyToast
  • 765
  • 3
  • 10
  • 18
  • Actually, I believe I have proven my own stupidity just now... – UnworthyToast Apr 19 '14 at 03:34
  • Why so, question seems to be OK? – Pranit Kothari Apr 19 '14 at 03:35
  • After looking at the code I realize the hidden argument C++ adds is a pointer to the instance of the class and not the argument. Nevertheless, I guess my question about whether or not there is a 'this' pointer for member functions without arguments still stands. – UnworthyToast Apr 19 '14 at 03:36
  • 4
    @WorthyToast trying to understand exactly how things work is a good attitude in a complex domain like programming. C++ has a steep learning curve and it is perfectly normal to get confused sometimes about things that will seem obvious once you have understood them. –  Apr 19 '14 at 03:59

2 Answers2

3

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.

Pranit Kothari
  • 8,997
  • 10
  • 55
  • 128
  • In cases of no arguments, is there any real purpose of the hidden argument? In other words, are there any examples off the top of your head that you can think of where `*this` would be used without any other arguments? – UnworthyToast Apr 19 '14 at 03:41
  • @UnworthyToast In your getter (which has no arguments), simply replace the line you have with `return this->m_nID;` for a trivial example – Brandin Apr 19 '14 at 04:22
  • @UnworthyToast There is confusion. `this` pointer is used to access member of object and not method. Even you do not have any parameter in function, you can still access all the member of object, isn't it? – Pranit Kothari Apr 19 '14 at 04:26
1

All class methods that aren't static get a this pointer.

This article gives some ideas about when you might want to use the this pointer.

The presence of the this pointer in C++ has to do with the calling convention. It means there are differences on how space on the stack and in cpu registers is reserved for parameters to functions.

Non static methods in C++ normally use the thiscall convention as opposed to other conventions (varying between compilers and architectures).