tags:

views:

147

answers:

4

I understand that 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...

+7  A: 

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
I'm validating here: returning *this doesn't return the reference of the object but a copy of the object?
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
Mark, your example is misleading. Try Foo }. Then you see that in fact *this is a REFERENCE.
ypnos
Thanks; I get it! :]
@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
@ypnos: *this being a dereferenced pointer or a reference has nothing to do with that. `Foo }` will return a reference, since you tell it to (by declaring Foo }` will return a copy of the object, as with any other type.
tiftik
A: 

Read the section in your book on pointers and dereferencing before you reread the section on this.

John Gordon
mean and should go in comments
aaa
I didn't mean it to be mean.
John Gordon
+6  A: 

this is a pointer to the instance of the class. *this is a reference to the same. They are different in the same way that int* i_ptr and int& i_ref are different.

Marcelo Cantos
A: 

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

Chris Hafey
I think you mean (*this).foo() since dot (.) has higher precedence than dereference (**).
John Gordon
ahh yes, guess i should have thrown that in the compiler first to verify :)
Chris Hafey