Sticky Interfaces

Started by
3 comments, last by Thrawn80 19 years, 1 month ago
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!
You'll never see heaven if you haven't been through hell.
Advertisement
oh sorry,

I forgot to add a * for my example.

IMyInterface *interface = new CMyChildRenderer();

Thanks a lot!
You'll never see heaven if you haven't been through hell.
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 comesclass 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.
"I am a servant of the Secret Fire, Wielder of the Flame of Anor; you cannot pass. The dark fire will not avail you, Flame of Udun! Go back to the Shadow. You cannot pass!” he said. With a bound, the Balrog leaped full upon the bridge. Its whip whirled and hissed. JRR Tolkin - Lord Of The Rings.
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}
Rob Loach [Website] [Projects] [Contact]
Icic... thanks for your help!!

Man ... this rocks!...
You'll never see heaven if you haven't been through hell.

This topic is closed to new replies.

Advertisement