1

我有一个抽象类,我从中继承。为了清楚起见-我在继承器中实现了所有抽象方法。

使用继承类创建的对象完成后,我试图破坏它,但不是调用继承的析构函数,而是将我发送到抽象方法。

在我的程序中它看起来像这样

class LinkedHash {

private:
      LinkedHashEntry **table;
      int max_size;
public:
      /* Creates a hashtable of size maxSize*/
      LinkedHash(int);
      LinkedHash(){}       
      /* deletes all members of the hashtable */
      virtual ~LinkedHash();

      virtual int hashFunction(int key) = 0;

      void insert(int key, Process* value);

      void remove(int  key);
      int getMaxSize(){return max_size;}
      LinkedHashEntry* search(int x);
};

class LinkedHashinheritor: public LinkedHash {

    public:
        LinkedHashinheritor():LinkedHash(1000){}
        int hashFunction(int key);
};

这是我的 cpp 文件相关代码:

int LinkedHashinheritor::hashFunction(int key)
{
    return key%1000;
}

并且有对相关对象的构造函数和析构函数的调用:

   Scheduler::Scheduler()
{tmp_lh=new LinkedHashinheritor();}

Scheduler::~Scheduler()
{
    delete tmp_lh;
}
4

2 回答 2

3

预先说明这一点:使类抽象并不会阻止调用其析构函数。将始终调用继承层次结构中所有类的析构函数(按照从派生类到基类的顺序)。

由于您没有在派生类中声明析构函数,并且编译器提供的析构函数非常简单(例如,它只需要从基类调用析构函数),因此编译器很可能已经优化了派生类析构函数。

于 2012-12-22T16:56:01.970 回答
2

请注意,virtual析构函数是特殊virtual函数:基类析构函数总是在销毁派生对象时被调用。制作析构函数的效果virtual是,您可以delete通过指向基类型的指针来创建派生类型的对象(如果在没有析构函数的情况下执行此操作,virtual则会得到未定义的行为。

于 2012-12-22T16:57:32.203 回答