1. 基类如果析构函数不为protected,那么最好加上virtual ,让其delete 基类指针 实现多态
2.如果基类析构函数为protected, 可以这样使用:
基类的构造,拷贝构造, 基类& operaotr = (基类指针) 均为protected, 不直接产生、销毁基类。
可以提供一个虚函数去销毁 {delete this:}
效率比纯虚基类效率高一些?
class Base
{
protected:
Base(){}
Base(const Base& another){}
virtual ~Base(){}
Base operator = (const Base & another);
public:
virtual void Release()
{
delete this;
}
virtual void fun() const;
};
void Base::fun() const
{
cout<<"Base::fun()"<<endl;
}
class Derived :public Base
{
public:
virtual void fun() const;
};
void Derived::fun() const
{
cout<<"Derived::fun()"<<endl;
}
int main()
{
Base* pBase = new Derived();
assert(pBase);
pBase->fun();
pBase->Release();
pBase = NULL;
return 0;
}