Polymorphism on Variables of class

How is the values of
J = p.i is from parent class (1) and not from child class (2)

Since all references to the base class variable p originate from the perspective of the Packet base class type, the reference p.i refers to the class member Packet::i and is unaware of anything declared in the LinkedPacket class.

Hi @dave_59

What exactly happens when we do
P = lp ;

My understanding is ,
That it will assign the handle of child class to parent class .
And parent class handle is equivalent to child class for all properties.

For methods it can be overridden in case of virtual.

Like any other assignment, p = lp copies the value on the right-hand side (RHS) and stores it in the variable on the left-hand side (LHS). In this specific instance, the value being copied is the class handle stored in the class variable lp. This handle, incidentally, serves as a reference to an instance of a LinkedPacket object.

Any class variable can hold a handle(reference) to a class object of the same class type or any of its derivatives. In this example code, we know the variable phas a handle referencing a LinkedPacket object. We wouldn’t know this is more complex code, and the LRM only allows references defined by the class type of the class variable storing the handle.

1 Like