Skip to main content
gamedev.net
🔒 Locked

Derivation

Started by Lightrocker Aug 14, 2002 at 3:03 AM 2 replies 250+ views
Original Post
Lightrocker
Lightrocker
I have declared two interfaces like that: interface IBase { virtual int FunctionOne( ... ) = 0; }; interface IExtended : IBase { virtual int FunctionTwo( ... ) = 0; }; Then there are two classes: class CBase : public IBase { public: int FunctionOne( ... ); }; class CExtended : public CBase, public IExtended { public: int FunctionOne( ... ); // without that line I get an error int FunctionTwo( ... ); }; This is a part of the implementation: int CExtended::FunctionOne( ... ) { return CBase::FunctionOne( ... ); } Is there a way, in which I don't have to define FunctionOne() in CExtended but can call it over the interface IExtended? The method of class CBase should be called directly. [edited by - Lightrocker on August 14, 2002 4:06:44 AM]
niyaw
niyaw
i know i''m too lazy to open msvc and test this out first, but try putting a using-directive in your CExtended class like so:

using CBase::FunctionOne;

i give no guarantees that this will work.
SabreMan
SabreMan
CExtended is inheriting a FunctionOne via two routes: CBase, and IExtended. The version of FunctionOne inherited via IExtended has no over-ride, so is still abstract. You seem to be expecting that CExtended will mix-in CBase and take the implementation of FunctionOne from there, but haven''t realised you have two different IBase objects within the overall object representation. To ensure that you only get one IBase within the object rep, you need to specify that derived classes are to share the same IBase, and you can do that using virtual inheritance. Change the IBase derivations to be:

interface IExtended : virtual IBase

and

class CBase : public virtual IBase
Lightrocker
Lightrocker
It was a good idea, niyaw, but it doesn''t work with pure virtual methods, which I need for a DLL interface.
Thank you, SabreMan, you are right and now my code is running like I want it to do.

Topic Locked

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

Sign in to reply to this topic.