+ 3

Does casting not adjust the pointer in c++

Hi I tried casting using dynamic cast and done object slicing also. In both case, pointer adress does not change. Does it not needed to get modified ? Even though derived has base details, changing from derived to base will not adjust the pointer? Refer code below: https://sololearn.com/compiler-playground/cbZLq6MlNhNL/?ref=app

16th May 2024, 4:31 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
13 Respuestas
+ 4
In case of single inheritance , it doesn't adjust the pointer, since objects with an simple inheritance hierarchy are stored within memory by placing the superclass <BASE> as the first item in the subclass <CHILD> and the new entries are tacked on the end. ( the same thing is true for the vtable ) But for multiple inheritance, it will have to adjust the pointer ( because subclass <CHILD> contains <BASE1> first in memory, then <BASE2> second, if we cast from <BASE1> to <BASE2>, we move the pointer in memory to point to the second base class ) : https://www.sololearn.com/en/compiler-playground/c5b5Y3quS4VE/?ref=app
17th May 2024, 4:32 PM
MO ELomari
MO ELomari - avatar
+ 4
Changing from a derived pointer to a base pointer (or vice versa) does not adjust the pointer address because the memory layout of the derived object ensures that the base part is located at the same address as the derived object. And about slicing object slicing occurs when you assign a derived class object to a base class object. However, this only applies to objects, not pointers or references.
17th May 2024, 5:21 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 4
Well, a derived class might have some more attributes and methods. Assigned to base class one could thing for sake of memory we drop the instance and create another one. Because with static type base class we have no access to it. Try it and create a further method fm in derived class, assign an instance of it to base class and try to call fm. But as you see: it is possible to change static type again -like you did. Therefore it mak3s no s3nse.
17th May 2024, 5:42 AM
Oma Falk
Oma Falk - avatar
+ 3
I do not know whether dynamic typecasting an object should adjust the pointer, though I think probably not. Regardless, it may well be that the subclass instance data starts at the location for the base class data, so that it includes base class data, and then extends beyond it. Therefore sub and base pointers would be the same. This is my hypothesis.
16th May 2024, 10:46 PM
Brian
Brian - avatar
+ 1
Ketan Lalcheta you only have one instance of the derived class. The address would reasonably point to the same place. Here I have a couple of instances. The location pointed changes accordingly. https://sololearn.com/compiler-playground/czlzQneQIAjE/?ref=app
17th May 2024, 2:52 AM
Bob_Li
Bob_Li - avatar
+ 1
Object slicing
17th May 2024, 4:00 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Derived has something extra compared to base. So does removing (slicing off) something from derived makes us point to base. As it is still pointing to derived full object, why derived class details are sliced off?
17th May 2024, 5:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta inheritance is more of concept of order of allocation (base, then derived) rather than contingency of memory space allocation. That's why I was confused about the term 'slice'. subclasses often have more functionality than parent classes, that's why casting is directional, because functionality can be lost.
17th May 2024, 7:47 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks MO ELomari . I was missing this and could not recall the case where pointer shifts. Could you please relink the link as it is broken for me on android app.
17th May 2024, 5:20 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
17th May 2024, 6:03 PM
MO ELomari
MO ELomari - avatar
0
Derived class is made up of base class. When we slice the derived to base, does not it adjust some pointers ?
17th May 2024, 3:03 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
slice?
17th May 2024, 3:55 AM
Bob_Li
Bob_Li - avatar
0
Ok, but what pointers are you referring to? i'm having a hard time imagining your expected outcome...
17th May 2024, 4:10 AM
Bob_Li
Bob_Li - avatar