标量删除析构函数

时间:2013-05-03 08:01:25

标签: c++ polymorphism destructor scalar delete-operator

我有一段代码(来自DLL),如下所示:

class A {
    public:
        virtual ~A();
};

class B : public A {
    public:
        ~B();
}

~A() {
    // #1
}

~B() {
    // #2
}

当我使用delete an_instance_of_B时,我得到scalar deleting destructor。任何解决方法?

1 个答案:

答案 0 :(得分:10)

不要delete B的实例。您应该在指向分配了delete的对象的指针上使用new

B b;
delete b;   //wrong

//.........

B* pB = new pB;
delete pB;  //okay

//.........

B justThis; //best