开发者

*this vs this in C++

开发者 https://www.devze.com 2022-12-29 00:43 出处:网络
I understand what this does, b开发者_如何学JAVAut what is the difference between *this and this?

I understand what this does, b开发者_如何学JAVAut 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...


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号