Virtual methods
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.
I'm assuming you are asking about C++. The answer is to call A::func() instead of just func().
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?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement