Archived

This topic is now archived and is closed to further replies.

Virtual base classes and overload augmentation

This topic is 5177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing a set of classes where the base class is an interface which declares a number of overloaded functions - that is,
class ISerializer
{
 virtual void IO(int &val)=0;
 virtual void IO(float &val)=0;
 virtual void IO(std::string &val)=0;
};
and so on. I then publicly derive from that class (e.g. ''class CSerialSaver : public ISerializer''), implement the virtual functions, and all seems to work. However, I want to be able to augment the system to add extra overloads - IO(long &val) for example - without changing the existing classes. So, what I thought I could do is this:
class ISerializerExtender : virtual public ISerializer
{
 virtual void IO(long &val)=0;
 virtual void IO(someOtherType &val)=0;
};
and *then,* derive objects from both that and the original concrete classes (so, ''class CExtendedSerialSaver : public ISerializerExtender, CSerialSaver''), implement the new overloads, and then it all be peachy. Then when I''m passing the extended objects around I use a pointer to ISerializerExtender rather than ISerializer. My question is, will this work, and have I set up the virtual base classes properly? I''m wondering if the CSerialSaver-level classes need to have ISerializer as virtual as well. Superpig - saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4 ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

Share this post


Link to post
Share on other sites
You''ll end up with two ISerializer subobjects if you mix virtual and public inheritance like that, one virtual from ISerializerExtender and the one non-virtual from CSerialSaver. To avoid that, only use virtual inheritance from ISerializer.

Virtual inheritance might be what you want anyway for ISerializer, but it seems unnecessary just for adding extra functionality - regular inheritance should suffice.

Share this post


Link to post
Share on other sites
OK, so I need virtual inheritance on CSerialSaver-level objects as well. Thanks!

dmikesell: I can''t use template classes because each overload is doing a fundamentally different operation - this is actually for a serialisation mechanism, and I want to be able to add overloads for serializing more complex structures (like vectors or matricies) without having to put serialisation operators in the structures themselves. It''s basically a Visitor pattern.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

Share this post


Link to post
Share on other sites