Hello, guys:
Recently, I got a question, about pure virtual class. Sometimes I need to design an interface for my job, looks like:
class CInterface
{
virtual ~CInterface()=0{}
virtual void SomeAction()=0;
}
class DerivedA:public CInterface
{
DerivedA(){}
~DerivedA(){}
void SomeAction()
{
cout<<"Action from Derived class A ."<<endl;
}
}
But what make me confused is that it works well after I change CInterface,like below:
class CInterface
{
virtual void SomeAction()=0;
}
So, my question is, shall I use destructor in a pure virtural class?And when?
Thanks in advance!
[ADD]:
Firstly, thanks a lot to all of you!
Then we should continue about this issue. In my opinion, what we design an interface or a pure virtual class for is use it as an interface or pointer(some address pointed to DerivedA object existed).
1. So, we do it like this:
void SomeFun(CInterface *pt)
{
pt->SomeAction();
}
2. Not used like this:
CInterface *pt = new DerivedA();
pt->SomeAction();
delete pt;
Because we don't design CInterface as a 'base' class for DerivedA but only an interface,if so ,we should call it 'DerivedABaseClass' or sth.
To make the class CInterface be a real interface, we only use it as a 'handle' in client classes,we should never allocate or deallocate upon 'CInterface*' pointers.
3. So, I think the destructor in class CInterface should be canceled.
4. Welcome for discussing, anyone make the issue clear will be appreciated.