+ 1

C++ Pointers to objects (objects of a child class)

Say I have a parent class and a child class (where the child class publicly inherited everything from the parent class). In some tutorials about polymorphism and virtual functions, they mention something similar to the code below: Parent *ptr = childobj; If the childobj is an object from the child class, does the above code imply: ptr stores the address of childobj BUT if you use this pointer, you can only access/modify the 'traits' of childobj that was inherited from it's PARENT class? Not the other 'traits' or methods that are ONLY defined in the child class???

21st Apr 2019, 6:36 PM
J3fro C
J3fro C - avatar
1 Resposta
+ 3
I believe it is like that... unless mommy's properties have a 'virtual' in front of them; in that case there should be a 'detour' to the stuff of the kid object. (This is new for me too, if I am wrong, please correct me.)
21st Apr 2019, 6:51 PM
HonFu
HonFu - avatar