相关疑难解决方法(0)

何时使用虚拟析构函数?

我对大多数OO理论有了深刻的理解,但让我困惑的一件事是虚拟析构函数.

我认为无论什么以及链中的每个对象,析构函数总是会被调用.

你什么时候打算让它们成为虚拟的?为什么?

c++ polymorphism shared-ptr virtual-destructor

1420
推荐指数
13
解决办法
66万
查看次数

是否有用于使受保护的析构函数虚拟化?

/*Child is inherited from Parent*/
class Parent {  
  public:  
    Parent () //Constructor
    {
        cout << "\n Parent constructor called\n" << endl;
    }
  protected:
    ~Parent() //Dtor
    {
        cout << "\n Parent destructor called\n" << endl;
    }
};

class Child : public Parent 
{
  public:
    Child () //Ctor
    {
        cout << "\nChild constructor called\n" << endl;
    }
    ~Child() //dtor
    {
        cout << "\nChild destructor called\n" << endl;
    }
};

int main ()
{
    Parent * p2 = new Child;          
    delete p2;
    return …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance protected virtual-destructor

10
推荐指数
3
解决办法
1万
查看次数