• Advertisement
Sign in to follow this  

Sticky Interfaces

This topic is 4739 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

Hi all, I think I'm lacking in some basic concepts of interfaces... For example : class IMyInterface { public : virtual void renderObject(void) = 0; virtual ~IMyInterface(void) {}; } class CMyParentRenderer : public IMyInterface { public : virtual void renderObject(void); } now comes : class CMyChildRenderer : public CMyParentRenderer { // Do I have to implement IMyInterface as well? } I have certain situations where I only need the parent class to implement the interface, but the child class uses the parent's functionality... 1) What happens if I do the above? Do I have to implement my parent class' interface as well? 2) If I do this, IMyInterface instance = new CMyChildRenderer(); delete(instance); 2a) will child renderer get deleted? 2b) if the child renderer also inherits another parent class, will this class be deleted as well? I'm in a confusion for interfaces.... help me please. Please advise. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
oh sorry,

I forgot to add a * for my example.

IMyInterface *interface = new CMyChildRenderer();

Thanks a lot!

Share this post


Link to post
Share on other sites
Quote:
What happens if I do the above? Do I have to implement my parent class' interface as well?


Yes, you must create all the virtual methods at least once.

for example:


class Ione { virtual void method1() = 0; };
class Itwo : public Ione { virtual void method2() = 0; };

//now comes
class Ithree : public Itwo {

// here in order to allow instantiation of this object, you must provide both of the above
// virtual mehtods Ione::method1() and Itwo::method2(). if Ione::method1() already defined in Itwo then you do
// not need to re define it.

};



Quote:
2a) will child renderer get deleted?
2b) if the child renderer also inherits another parent class, will this class be deleted as well?


yes, Ione * p = new Itwo; delete p; will delete the full Itwo object correctly.
the same case is applied to Ione * p = new Ithree; ...

Hope this helps.

Share this post


Link to post
Share on other sites
You don't have to derive CMyChildRenderer from IMyInterface because CMyParentRenderer is already derived from IMyInterface.


class IMyInterface
{
public :
virtual void renderObject(void) = 0;
virtual ~IMyInterface(void) {};
virtual void Shake(){};
}

class CMyParentRenderer : public IMyInterface
{
public :
virtual void renderObject(void);
}

now comes :

class CMyChildRenderer : public CMyParentRenderer
{
void renderObject(){ Shake(); } // Works
}

Share this post


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

  • Advertisement