Virtual Classes
-
When inheriting a virtual class, do I have to completely redefine virtual functions of the base class. For instance, does the function following COMENT A work, or do I have to completely rebuild the operator = function? Thanks. class Base{ public: Base(); ~Base(); virtual Base& operator = (const Base& base); }; class Inherited : virtual public Base{ public: Inherited(); ~Inherited(); virtual Inherited& operator = (const Inherited& inherited); }; //COMENT A Inherited& Inherited::operator = (const Inherited& inherited){ if (&inherited != this){ Base::operator = (inherited); //do some other copying } return *this; }
-
When inheriting a virtual class, do I have to completely redefine virtual functions of the base class. For instance, does the function following COMENT A work, or do I have to completely rebuild the operator = function? Thanks. class Base{ public: Base(); ~Base(); virtual Base& operator = (const Base& base); }; class Inherited : virtual public Base{ public: Inherited(); ~Inherited(); virtual Inherited& operator = (const Inherited& inherited); }; //COMENT A Inherited& Inherited::operator = (const Inherited& inherited){ if (&inherited != this){ Base::operator = (inherited); //do some other copying } return *this; }
Why are you using
virtual
inheritance here? Generally, you inherit virtually when you're using multiple inheritance, and when those base classes share some common base class at some point. For single inheritance, as in your example, you don't need to use virtual inheritance when declaring the class. For your example, why not try compliling and single-stepping through the code? That way, you'll know if it works or not. Personally, I'm not sure :) Bob Ciora -
Why are you using
virtual
inheritance here? Generally, you inherit virtually when you're using multiple inheritance, and when those base classes share some common base class at some point. For single inheritance, as in your example, you don't need to use virtual inheritance when declaring the class. For your example, why not try compliling and single-stepping through the code? That way, you'll know if it works or not. Personally, I'm not sure :) Bob CioraHi. Thanks for your response. I am using multiple inheritance. I just didn't want to burden a potential responder with reading unecessary code. With respect to your comment, I have stepped through, and it doesn't appear that I have to completely redefine the function. I can call the base function in the derived function even when the base function is virtual and it calls the right (or wrong depending on your view) version. The documentation suggests this should not be the case.