aRef has a Window static type, but CommandButton dynamic typebRef is just an object of type Window (the CommandButton part is "lost in copy )
It is commonly known as object splitting , and this is usually prevented by creating base classes either abstract (by providing a clean virtual function) or not copying (for example, using boost::noncopyable ), because any solution will make the code not compiled in the Window& aRef = *button; .
Now, why does bRef.Create() call Window::Create ? Well, bRef has nothing more than a Window , so there really isnβt so many alternatives. This essentially looks like declaring a Window and calling Create on it: the fact that bRef was copied from an instance of CommandButton does not matter, because part of the CommandButton was lost in the copy.
I will try to make this clearer by specifying the standard (10.3 / 6):
[Note: the interpretation of a virtual function call depends on the type of object for which it is ( dynamic type ), whereas the interpretation of a non-virtual member function call depends on only the type of pointer or reference designating this object ( static type ) (5.2.2). ]
Only using a pointer or reference can a static type of an object differ from its dynamic type.
icecrime
source share