52

I understand what this does, but what is the difference between *this and this?

Yes, I have Googled and read over *this in my text book, but I just don't get it...

DavidRR
  • 18,291
  • 25
  • 109
  • 191

3 Answers3

89

this is a pointer, and *this is a dereferenced pointer.

If you had a function that returned this, it would be a pointer to the current object, while a function that returned *this would be a "clone" of the current object, allocated on the stack -- unless you have specified the return type of the method to return a reference.

A simple program that shows the difference between operating on copies and references:

#include <iostream>

class Foo
{
    public:
        Foo()
        {
            this->value = 0;
        }

        Foo get_copy()
        {
            return *this;
        }

        Foo& get_copy_as_reference()
        {
            return *this;
        }

        Foo* get_pointer()
        {
            return this;
        }

        void increment()
        {
            this->value++;
        }

        void print_value()
        {
            std::cout << this->value << std::endl;
        }

    private:
        int value;
};

int main()
{
    Foo foo;
    foo.increment();
    foo.print_value();

    foo.get_copy().increment();
    foo.print_value();

    foo.get_copy_as_reference().increment();
    foo.print_value();

    foo.get_pointer()->increment();
    foo.print_value();

    return 0;
}

Output:

1
1
2
3

You can see that when we operate on a copy of our local object, the changes don't persist (because it's a different object entirely), but operating on a reference or pointer does persist the changes.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 1
    I'm validating here: returning *this doesn't return the reference of the object but a copy of the object? –  May 01 '10 at 14:41
  • You are right to ask. *this in fact returns a reference to the object. For example if you overload the [] operator in your class you can inside the class use (*this)[] instead of operator[]. – ypnos May 01 '10 at 14:45
  • 5
    Mark, your example is misleading. Try Foo& get_copy() { return *this; }. Then you see that in fact *this is a REFERENCE. – ypnos May 01 '10 at 14:48
  • @ypnos: I see what you mean and I have clarified my answer to differentiate between returning `*this` as a copy and as a reference. – Mark Rushakoff May 01 '10 at 14:54
  • 2
    @ypnos: *this being a dereferenced pointer or a reference has nothing to do with that. `Foo & get_copy() { return *this; }` will return a reference, since you tell it to (by declaring Foo &). And `Foo get_copy() { return *this; }` will return a copy of the object, as with any other type. – tiftik May 01 '10 at 15:07
  • 2
    To be more precise, the dereference of a pointer results in an *lvalue* that refers to the object. Since it is an lvalue, you can assign it to a reference variable and get a reference to it; or you can assign it to a variable of the object's type and get a copy, or you can do `&` on it to get the pointer again. – newacct Feb 26 '13 at 01:24
  • @MarkRushakoff: so, when we overload = (assignment) operator it should return reference to clone of the object. Right? – Destructor May 02 '15 at 11:13
2

Case 1

this is a pointer to an object of a class, on which the non-static member function was called. Moreover, when used as an expression the value-category of this is prvalue.

When we call a non-static member function on an object of the class, the address of that object is implicitly passed as the first argument of that member function. This is possible because every non-static member function has an implicit parameter named this which is of type X* for a nonconst member function and const X* for a const member function, where X is some class.

Case 2

When we apply * on this the result is the object to which the pointer this was pointing. We are basically dereferencing the this pointer. That is, *this gives us the object to which the pointer this points. In other words, we get the object on which we called the non-static member function.

Moreover, when used as an expression the value category of *this is lvalue. Also, note that

The type of an expression is never a reference.

Note that lvalue expression is different from lvalue reference. The answer by Marcelo Cantos and comment by cigien seems to be confusion these two concepts.

Summary

this *this
Pointer to an object of a class on which the non-static member function was called. Result of dereferencing the this pointer. The result is the actual object on which the non-static member function was called.
prvalue-expression lvalue-expression
Jason
  • 36,170
  • 5
  • 26
  • 60
-8

There is no real difference, this->foo() is the same as (*this).foo().

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Chris Hafey
  • 601
  • 3
  • 5