-2

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? 
  • 1
    It's not an upcast because that's how C++ works. You're attempting to stuff 10 pounds of potatoes into a 5 pound bag. – PaulMcKenzie Aug 13 '14 at 16:48

2 Answers2

4

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.

Rob K
  • 8,757
  • 2
  • 32
  • 36
1

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

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63