If I have an object * pointing to SoftBall, will the foo () version of SoftBall be called? If not, is there a way to achieve this effect? Basically, does this aspect of polymorphism still work on more than one level of inheritance?
Yes
C ++ defines methods as virtual if the corresponding overload is virtual in the base class (unlike other OO languages, where overrides must be explicitly marked).
http://codepad.org/pL09QWNN
Please note that with some additional ingredients you can get really scared:
Try to predict what is printed here. This gets a lot more interesting when you mix:
- (mutable / const / volatile qualified) overloads
- (conflicting) default arguments
I remember that I read quite a few more and less terrifying examples in the form of a trifling question, which I hope never to meet in an interview. Although I know what I would answer: "If you have this code, I'm not sure I want a job" ?.
You can take a look at Herb Sutter, Scott Meyer, and you will be amazed that the traps are hidden in our otherwise-civilized-good-less-lingual-loving-somehow in C ++
sehe
source share