0

I was trying to understand how the this and the *this keywords work in C++. From my understanding, this returns a pointer to the current instance of the object for which it is called, whereas *this returns a clone of the very same instance.

I saw a version of the below code being used in an algorithms question in a different, unrelated place. I have only kept the parts of the code that pertain to my query.

#include <iostream>

class Base
{
    public:
    int a, b;
    
    void haha()
    {
        std::cout << "haha!!!" << std::endl;
    }
};

class Derived : public Base
{
    public:
    int c, d;
    
    void method()
    {
        Base b(*this);
        b.haha();
    }
};

int main()
{
    Derived d;
    d.method();

    return 0;
}

I am not able to wrap my head around how *this (a copy of the current Derived class object) is being used to instantiate an object of the Base class.

What is the OOP principle in play here?

  • 2
    object slicing and copy constructor. The fact that this method is inside `Derived` is irrelevant. – Rotem Apr 25 '22 at 17:34
  • [What is object slicing?](https://stackoverflow.com/questions/274626/what-is-object-slicing) – user4581301 Apr 25 '22 at 17:35
  • 1
    Note that as `this` is a pointer to the current instance, `*this` isn't a copy, but the current instance itself (or a reference to it if you wish). This is not particularly relevant to this question or the answer, just something to keep in mind. – Lala5th Apr 25 '22 at 17:36
  • 3
    `*this` doesn't return a clone of the object, it returns a reference (which can be used to make a copy, sure). Then, you use that to call implicitly declared copy constructor `Base(const Base&)`. You can pass `Derived&` there, because `Derived` is a `Base`, but `Base` copy constructor knows nothing about `c` or `d` and copies simply `a` and `b`, as if it got a `Base&`. And that's pretty much it. – Yksisarvinen Apr 25 '22 at 17:40
  • oh, you return a void. – apple apple Apr 25 '22 at 18:08

1 Answers1

1

Both this and *this are not functions and therefore do not return anything. *this refers to the object pointed to by this. There is no copying involved by just writing

*this; // yes, that's a valid statement, although it has no effect

What would constitute a copy would be one of these:

Derived copy1(*this);
Derived copy2{*this};

However, the copying is done just as it would be if you called the constructor outside of the Derived member function:

Derived copy{d};

However, since Derived is a Base, writing Base b(*this); will call the copy constructor of Base with the Base sub-object of *this. That copies the Base portion, i.e. members int a and int b. It would be like doing this:

Base& dref{d}; // does not copy, slices d
Base b{dref}; // now we copy d as if it was a Base

Note that calling Base::haha would have always printed "haha!!!" because Derived inherits that function and thus calling it without creating a Base copy would have called the same function.

bitmask
  • 32,434
  • 14
  • 99
  • 159