Jump to content
  • Advertisement
Sign in to follow this  
Puppet

Inheriting interface implementations

This topic is 4732 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 must say I'm not too good at the specifics of C++. If I have some pure virtual interfaces that most of my objects will inherit (faking my own COM like system). Many of the object implementations of these interfaces will be identical. So I'd like to reuse some common code somehow. Simplified it looks something like this:
// COM-like base interface that all objects inherit
class IUnknown
{
public:
virtual bool AddRef() = 0;
//.. queryinterface etc

};

// An IEntry interface has some database like functionality
class IEntry : public IUnknown
{
public:
virtual u32 ID() = 0;
//.. other stuff
};

// An interface to do some rendering
class IView : public IEntry
{
public:
virtual void Render() = 0;
//.. etc
};

// This is some common code, implementing some stuff that
// won't really differ much between objects
class EntryDefImpl
{
protected:
u32 m_id;
public:
u32 ID() { return m_id; }
//..etc
};

// My final object, implementing all remaining functionality
// of the interfaces
class MyView : public EntryDefImpl, public IView
{
public:
void Render();
//.. more code and specifics here
};

This doesn't seem to be the way to do it. Inheriting EntryDefImpl doesn't seem to implement the virtual functions, the complier complains they are not. I guess inserting EntryDefImpl kinda lika an part of the inheritence chain is not right. I'd just like to be able to insert some default implementations of some interface functions. It's not necessary for MyView to be castable to EntryDefImpl or anything. Now I'd like to solve this without any hackery like #include:ing an entire sourcefile in the middle of a class. Is there any way to just get the compiler to implement some virtual functions from some common code? I know I can inherit EntryDefImpl from IEntry and IView from EntryDefImpl, but that's just not right. I want all the interfaces leading up to my actual object to be pure virtual interfaces. I hope I'm making sense. Thanks, Christer

Share this post


Link to post
Share on other sites
Advertisement
Quote:
I know I can inherit EntryDefImpl from IEntry and IView from EntryDefImpl, but that's just not right. I want all the interfaces leading up to my actual object to be pure virtual interfaces.


Since you're using multiple inheritance, you should familiarize yourself both with domination and with virtual inheritance.

Note that the functions in EntryDefImpl haven't been declared virtual. That's probably an error.

What are the error messages the compiler gave you?

Share this post


Link to post
Share on other sites
I found if I use:



public IView : public virtual IEntry
{
//..
};

public EntryDefImpl: public virtual IEntry
{
//..
};





It works, by something called dominance (compiler throws some warnings about which implmentations are used). Now is this a bad thing or is this the way to do it? Will the compiler just "copy and paste" code from the different virtual function implementations into the concrete object, without any additional performance issues (besides the virtual inheritance)?

Share this post


Link to post
Share on other sites
Should I always use the keyword virtual when inheriting a pure virtual interface?

Like so:
"class MyClass : public virtual IMyPureVirtualInterface"

(Fruny: The compiler error was simply that it couldn't instanciate the abstract MyView class, because the functions implemented by EntryDefImpl weren't recognized as implemented)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!