Class Inheritance Question
Hey there,
Just a quick question about class inheritance:
If I have a class C that inherits from both class A and class B.
Class A and class B have a virtual function called foo()
To avoid issues in class C I would put this as the declaration as C::foo()
Code:
virtual void C::foo(){ A::foo(); }
To make sure it ran A's foo() instead of B's
Is this the correct way to do things? Or should I declare B's foo() without the virtual directive? Even if A's data members are declared as protected and are easily accessible in class C. Should I leave them both as virtual in class A and B and just copy class A's foo() into class C's foo()?
I guess what I'm asking is what normally happens in this situation.
Thanks,
Lang
Re: Class Inheritance Question
This is fine:
Quote:
Code:
virtual void C::foo(){ A::foo(); }
You are being explicit which is a correct solution.
Re: Class Inheritance Question
Do A and B have a common base class? An Abstract base?
Problem #1 If not, you may want to test your code to make sure that the correct underlying member variables are being manipulated.
Have a look at virtual public inheritance. You will see the inheritance diamond and see the way that having virtual public inheritance when using multiple inheritance makes sure that problem #1 does not occur. Virtual public inheritance also may cause some performance hit.
Re: Class Inheritance Question
Quote:
If I have a class C that inherits from both class A and class B.
Multiple inheritance with the same function name in both classes is asking for trouble.
Code:
class C : public A, public B
{
foo ();
};
C myclass;
myclass.foo ();
Which one is called ?
Re: Class Inheritance Question
well if A::foo() and B::foo() are both virtual then they are meant to be overridden. We need to see more of this guys code to know exactly how to respond, or more about his class structure.
so if C::foo() explicitly calls A::foo() this is fine, as long as the rest of his class structure supports it.
Re: Class Inheritance Question
Lang please post your class code!:)
Thank You!