The code you’ve posted should work the way you want. Calling doSomething
on an instance of derived
will call the overridden start
and stop
functions defined in derived
.
There’s an exception to that, though. If you call doSomething
in the constructor or destructor of base
(whether directly or indirectly), then the versions of start
and stop
that get called will be the ones defined in base
. That’s because in those circumstances, you don’t actually have a valid derived
instance yet. It’s either not fully constructed or partially destructed, so the language prevents you from calling methods that would use the partial object.
If you’re not calling it from a base
constructor or destructor, then there is more to the problem than what’s shown here.