Is this an upcast or not? If not, please describe why. Thanks in advance.
C++ code:
Base base;
Derived derived;
base = derived; // is this the upcast?
Is this an upcast or not? If not, please describe why. Thanks in advance.
C++ code:
Base base;
Derived derived;
base = derived; // is this the upcast?
No, because you're not assigning a pointer or a reference, you're trying to assign an actual instance. That is, you're trying to copy the content of derived
into the memory where base
exists. What you'll end up with is slicing, only copying the contents of the Base
part of derived
.
This should be covered in any decent C++ book.
Not really.
An implicit "upcast" can be some thing like
Derived derived;
Base& base = derived;
Note that Base&
is not another Base
object, but a reference to some other Base
(the one contained in derived
)
What you did, in fact is creating another Base and copying in it that Base sub-component of derived.
That still requires an implicit upcast, but from then on, your base
is not anymore related to derived