Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++] Inheritance Diamond Problem
#4
(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.
Reply
Thanks given by:


Messages In This Thread
RE: [C++] Inheritance Diamond Problem - by A-Man - 01-06-2016, 09:10 PM
RE: [C++] Inheritance Diamond Problem - by sadbhav - 01-08-2016, 04:57 AM



Users browsing this thread: 1 Guest(s)