01-08-2016, 04:57 AM
(This post was last modified: 01-08-2016, 04:59 AM by sadbhav.
Edit Reason: whoops
)
(01-06-2016, 09:10 PM)A-Man Wrote: I suppose you're posting this to discuss the problem rather than look for an answer?
Interesting. I expected it would return 0 since it's being casted to Root, but no matter how I explicitly cast it to Mid2 after it was casted to Root, it would still output 1 for some reason. It's as if the Top object completely 'forgets' the virtual method of Root.
When we declare a virtual function and redefine it in a derived class; it is ensured that the compiler will call the redefined function from the derived class. It will happen even if we call that function with a pointer or reference to the base class of the object.
IBM even has a small page of this, I think. I can find and link it, if you want.
On a related note, the only way to call the base class function would be to do it explicitly using the scope resolution operator.
Probably. I'm not 100% on that one.
If at first you don't succeed, you're probably not going skydiving again.