Archived

This topic is now archived and is closed to further replies.

Inheritance and the THIS Pointer

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the situation:- Base Class is called Framework, the derived class from this is named Application. Now in the base class Framework I have a global pointer which is initialised to zero. It is a pointer of type base class, IE. Framework *pFramework = 0;. I then have the pFramework pointer hold the This pointer, IE. pFramework = This;. This is done in the constructor of the base class Framework. Question 1: I think when the derived object is being instantiated pointer pFramework is holding the THIS address of the derived object due to the fact the base class constructor is being called from the derived object. Let me explain, I am instantiating an object from the derived class so the base class is called and as such the constructor is called. * I have looked at the memory address of the derived class object instantiated and the pFramework pointer memory address which is holding the THIS address and they are the same. Hence the above conclusion. Is this correct?

Share this post


Link to post
Share on other sites
The this pointer always has, as its type, a pointer to the class defined by the enclosing scope. That is to say, in a Framework function, the type of this is Framework *. In an Application function the type of this is Application *. Now, coincidentally, the numerical value of a pointer to a derived class is often the same as that of the numerical value of that pointer when cast to the type of the base class, but this is not always the case. For example, under multiple or virtual inheritance, pointers to base classes often have different numerical values as compared to the pointer to a derived class.

So if I''m understanding your question, then I''d would say that your observation is based on coincidence. The pFramerwork pointer should be pointing to a the Framework portion of the derived class, which just so happens to also be the same as the address of the class when taken as an Application object under your current compiler with the current inheritance hierarchy.

Share this post


Link to post
Share on other sites
Ah just coincidence!!

I noticed when using the pFramework pointer that I can only access members from the base Framework class.

In the Application .cpp file when I instantiate an object of the Application (derived) class. Even though it creates an object combining both the Framework (base class) and Application (derived class) members. The THIS pointer does not point to that object but to the memory location of the Framework class (base class). Due to the reasons:-

A. It is in the Framework .cpp file (global variable).
B. The pointer is of the Framework class type as declared in Framework* pFramework;. And Application class and Framework class are two different types!!

Correct???

I think I got it.

Share this post


Link to post
Share on other sites
Keep in mind that the pointer is still a pointer to the Application object, it''s just that it points to the "Framework part" of the Application object. Which means, for example, virtual functions declared in Framework that are overriden in Application will still call the Application version. Also, in this case, you can dynamic_cast the pointer to an Application pointer and get a valid result.

Share this post


Link to post
Share on other sites
I got the THIS pointer as pointing to the address of the class. Now by declaring pFramework* = THIS in the Framework constructor does that then mean the pFramework pointer points to the Framework class members of the derived class object.

In other words is it ONLY because THIS is passed into the pFramework pointer in the Framework class constructor that the pFramework pointer holds the address of the Framework section of the instantiated derived class object?

Share this post


Link to post
Share on other sites