Skip to main content
gamedev.net
🔒 Locked

Virtual methods

Started by Thomo 1 reply 950+ views
Original Post
Thomo
Thomo
A base class, A has a virtual function, virtual func(); Class B derives from A and B overrides A with it's own func(). Class C derives from class B. How do I tell class C to use the func() in A and not that in B.
amorita
amorita
I'm assuming you are asking about C++. The answer is to call A::func() instead of just func().
Xai
Xai
and you would put this code inside another override in class C.

C::func()
{
A::func();
}

of course this is a fairly good sign that something in your design is not quite right, because the idea of C deriving from B indicates C is more like B than A. Of course this is not always true. And definately not true if you are doing something else in addition to A::func() in C. But how does class C know it doesn't want what is in B's version ... and how will you know 6 months from now when you change B?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.