During upcasting, Am I upcasting the derived class pointer/reference to base class pointer/reference to a derived class pointer/reference rather than upcasting to the type of object itself?
1 2
//For example
Base* BasePtr = &d1;
(&d1 Upcasted from Derived* to Base*)
Base& BaseRef = d1;
(d1 upcasted from Derived to Base&)
1 2
Derived* d2 = new Derived
Base* BasePtr2 = d2;
(d2 upcasted from Derived* to Base*)
d1 and d2 is being upcasted to a reference or pointer rather than the type of the object itself?
Yes, "upcasting" works through references or pointers. You're not upcasting "to the type of the object itself", if I'm understanding your question correctly.
The alternative would be copying, e.g.
1 2
Derived derived_obj;
Base base_obj = derived_obj;
and this is known as object splicing, which you do not want.
yes, my question is you cannot cast an object or pointer of an object to another object but you can only cast it to the base class pointer/reference right?
I have another question, When I do upcasting I am not changing the original type of the object right? I just changing the way of how the program is treating the object because the object type is fixed at compile time and cannot be changed at runtime?
d1 is still a derived type object but I just changing the way the program is treating d1 when I upcast it from (Derived to base&).
no new object is being created during upcasting or downcasting
I am just creating a new pointer or new reference during upcasting/downcasting.
is my understanding correct?
To use it polymorphically, yes, you can only cast it to the base class pointer/reference.
When I do upcasting I am not changing the original type of the object right?
Correct. You're not changing the original type of the object. You're just pointing to or referencing the actual object.
d1 is still a derived type object but I just changing the way the program is treating d1 when I upcast it from (Derived to base&).
You are understanding the concept correctly.
no new object is being created during upcasting or downcasting
I am just creating a new pointer or new reference during upcasting/downcasting.
is my understanding correct?
Correct. You can verify this by looking at the address of d1 and comparing it to BasePtr and the address of BaseRef.