What is the logic behind the THIS pointer

Hi!
I was looking into the this pointer I found lots of information.
But nothing behind it's logic as in why should i use it,how is it used.
Also I did find an old post about this but I still don't understand it.
Can someone please explain in detail please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

class Complex
{
    double real;
    double imag;

    public:
    // constructor
    Complex(double Real, double Imag): real(Real), imag(Imag){}

    // return the modified Complex object
    Complex conjugate()
    {
        imag *= -1.0;
        return *this;
    }

    // display the Complex number
    void print(){ cout << "(" << real << ", " << imag << ")"; }
};


int main()
{
    Complex z(2.0, 3.0);
    z.print();

    // z is modified by conjugate() then the returned instance calls the print()
    z.conjugate().print();

  cout << endl;
  return 0;
}
The this pointer is a pointer to an object of the class and the first invisible parameter of each member function. Basically:

1
2
3
4
5
    Complex conjugate(Complex *this)
    {
        imag *= -1.0;
        return *this;
    }
or
1
2
    Complex z(2.0, 3.0);
    z.print(); // -> print(&z); 
Let‘s assume you‘ve gote a class, for example
1
2
3
4
5
6
class MyClass {
private:
    // something here
public:
    // some stuff here
};


You can easily define an instance of that class:
1
2
3
4
5
int main()
{
    MyClass m;
    return 0;
}


And you can easily get the memory address of that object (i.e. of that instance of MyClass):
1
2
3
4
5
6
7
int main()
{
    MyClass m;
    MyClass* p_to_m = &m; // &m == memory address of m
    
    return 0;
}


So, getting the address of an instance of a class from outside the class looks pretty simple.
But how do you get the address of an instance of a class from inside the class definition?
In other words, if you want your class to say: here you are my address (the address of this instance), how can you achieve that?
That‘s what is this --> the address of the current instance of the class.

If you are asking what it could come at handy for, maybe super simple examples are hard to find, but its usage is pretty common.
For example, the overloaded prefix operator++ is usually somewhat like:
1
2
3
4
5
6
7
8
9
10
11
class MyClass {
    int myproperty {};
public:
    // some stuff here
    MyClass& MyClass::operator++()
    {
        myproperty++;
        return *this;   // the content of the memory address of this instance
                        // of this class
    }
};


In your code:
1
2
3
4
5
class Complex
{
    double real;
    double imag;
    ...


1
2
3
4
int main()
{
    Complex z(2.0, 3.0); // now real == 2.0 and imag == 3.0
    z.conjugate()


The coniugate() method returns an instance of class Complex:
1
2
3
4
5
6
7
8
9
10
// return the modified Complex object
Complex conjugate()
{
    imag *= -1.0;
    return *this;   // an instance of Complex is returned.
                    // Which one? The one at the memory address of z.
                    // Yes, it means a copy of z. If the function declaration were 
                    // Complex & conjugate()
                    // it would have been z itself.
}


1
2
3
4
5
int main()
{
    Complex z(2.0, 3.0);
    z.conjugate().print();  // since coniugate() returns an instance of Complex,
                            // you can invoke print() directly from there. 

I t think I get it! also this is not my code.
It's a code from someone else who went on to explain to another curious user about this pointer.
He got it but I didn't.
That's why I asked this question.
Thank you!
I've given a simplified answer, even because it would have been impossible for me to explain this topic correctly in my poor English, so you'd better search for a 'real' tutorial.
And, for precision sake, this is not “the address of the current instance of the class” as I wrote above, but a pointer which points to the address of the curr... Ok, I think you got it :-) I boiled it down to the essential.
Topic archived. No new replies allowed.