Home > database >  how does the 'this' pointer work in inheritance in c ?
how does the 'this' pointer work in inheritance in c ?

Time:06-10

questions:

  1. in the code below, the 'this' pointer points to different things in base and derived classes, even though it technically contains the same address. why is that.
  2. what is happening when the 'this' pointer is used with inheritance. (any help would be appreciated. thanks.)
#include <iostream>
using namespace std;

// this pointer and inheritance.
class base {
public:
  int x;
  base() {
    x = 10;
  }
  void func() {
    cout<< this->x << endl; // 10
    cout<< this << endl;
  }
};
class derived : public base {
public:
  int x;
  derived() {
    x = 20;
  }
  void func() {
    cout<< this->x << endl; // 20
    cout<< this << endl;
    base::func();
  }
};

int main () {
  derived d;
  d.func();

  cout<< "execution complete.\n";
  return 0;
}
output:
20
0x61ff18
10
0x61ff18
execution complete.

CodePudding user response:

  1. in the code below, the 'this' pointer points to different things in base and derived classes, even though it technically contains the same address. why is that.

Classes contain subobjects. They can be base-subobjects or non-static data members. The first sub-object can share the memory address of the encapsulating complete object:

|derived  |  < complete object
|base|x   |  < sub objects of derived
|x   |       < sub objects of base sub object

^
|
same address    
  1. what is happening when the 'this' pointer is used with inheritance.

If you call a non-static member function of a class, this points to the instance argument within that function.

If you call a non-virtual non-static member function of a base of a derived object, this will point to the base sub object within that function.

If you call a virtual non-static member function of a class using unqualified lookup, it will call the most derived override of that function and this within that function will point to the object where that function is derived.

CodePudding user response:

Your class derived contains two integers. They have the names:

this->base::x

and

this->derived::x

What you seem to be discovering is that you can simply type this->x to refer to one of those integers.

The compiler will decide which x you are referring to by context. Inside the base class, you must mean this->base::x. Inside the derived class, it is assumed that you mean this->derived::x and you would need to explicitly write this->base::x to tell the compiler that you are referring to the int x; defined in base.

the 'this' pointer points to different things in base and derived classes

That is not true.

this does not point to different things in base and derived. It is the same this.

The meaning of this->x changes, because your class has two variables both named x.

CodePudding user response:

The derived class in your program has a member function named func that hides the inherited member function with the same name from base. Similarly, the derived class also a data member named x that hides the inherited data member with the same name from base.

Now, when you wrote:

d.func(); //this calls the derived class member function `func` implicitly passing the address of `d` to the implicit `this` parameter

In the above call, you're calling the derived class' member function func meanwhile implicitly passing the address of d to the implicit this parameter. The type of this inside func of derived is derived*.

Now, inside the member function func of derived the expression this->x uses the data member x of the derived class(and not the inherited member x) because as explained above, it hides the inherited member from base. Thus we get the output 20.

Next, cout<< this << endl; just prints this.

Next, the call expression base::func() is encountered. Also note that base::func() is equivalent to writing this->base::func(). This has the effect that it calls the inherited member function named func that was hidden due to the derived class having a function with the same name. Note in this call also, the same address is passed to the implicit this parameter of func of base class. The difference this time is that, this inside base's member function func is of type base*.

Now, inside the member function func of base, the x in the expression this->x refers to the base class' data member x and hence prints the value 10. Thus we get the output 10.

Finally, we have cout<< this << endl; inside base's member function func, which just prints the this.

  • Related