Sticky Interfaces
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!
oh sorry,
I forgot to add a * for my example.
IMyInterface *interface = new CMyChildRenderer();
Thanks a lot!
I forgot to add a * for my example.
IMyInterface *interface = new CMyChildRenderer();
Thanks a lot!
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.
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}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement