我的虚析构函数不会在抽象类中调用

My virtual destructor will not be called in a abstract class

本文关键字:抽象类 调用 析构函数 我的      更新时间:2023-10-16

我的程序不会调用基类的虚析构函数。我发现了这个方法http://www.programmerinterview.com/index.php/c-cplusplus/virtual-destructors/所以我不知道怎么了。可以找人帮忙吗

#include <iostream>
#include <vector>
using namespace std;
class Number{
    private:
        int *arr;
        int n;
    public:
        Number(int *a, int i):
            arr(a),n(i){}
        virtual vector<int> operator()()=0;
        int* getArr(){return arr;}
        int getValueAt(int i){return arr[i];}
        int size(){return n;}
        virtual ~Number(){ delete [] arr;
            cout<<"In Number destructor"<<endl;
        }
};
class Odd: public Number{
    public:
        Odd(int *ar,int n1):
            Number(ar,n1){}
        vector<int> operator()();
        ~Odd(){}
};
class Even: public Number{
    public:
        Even(int *ar,int n1):
            Number(ar,n1){}
        vector<int> operator()();
        ~Even(){}
};
/*
 *      operator()()
 *  -input
 *      none
 *  -description
 *      returns a vector with all the
 *      odd numbers. This will be sent in
 *      as a function parameter to a
 *      higher order function
 *  -output
 *      vector<int> with odd values
 */
vector<int> Odd::operator ()()
{
    vector<int> temp;
    for(int i=0; i<Number::size(); i++){
        if(Number::getArr()[i]%2==1)
            temp.push_back(Number::getValueAt(i));
    }
    return temp;
}
/*
 *      operator()()
 *  -input
 *      none
 *  -description
 *      returns a vector with all the
 *      even numbers. This will be sent in
 *      as a function parameter to a
 *      higher order function
 *  -output
 *      vector<int> with even values
 */
vector<int> Even::operator ()()
{
    vector<int> temp;
    for(int i=0; i<Number::size(); i++){
        if(Number::getArr()[i]%2==0)
            temp.push_back(Number::getValueAt(i));
    }
    return temp;
}
/*
 *      filter()
 *  input:
 *      Takes a number object
 *  description:
 *      filter is a higher order function and
 *      it will return a vector with depending
 *      how the operator() is defined
 *  output:
 *      vector<int>
 */
vector<int> filter(Number *a)
{
    return (*a)();
}
/*
 *      addAll()
 *  input:
 *      Takes a number object
 *  description:
 *      addAll is a higher order function and
 *      it will return sum of a vector returned
 *      from a filtering process that depends on
 *      the definition of operator() being invoked
 *  output:
 *      vector<int>
 */
int addAll(Number *a)
{
    vector<int> temp=(*a)();
    int sum=0;
    for(unsigned int i=0; i< temp.size();i++){
        sum+=temp[i];
    }
    return sum;
}
/*
 *  This program was written to explore functional programming
 *  concepts in c++. One such concept is high order functions
 *
 *  I created an abstract Number class which contains an int
 *  array and size of the array. I also made two derived classes
 *  that define the virtual operator() function. I pass a 
 *  polymorphic pointer variable to the higher order functions
 *  and they call the overloaded operator()() which is dynamically
 *  binded during runtime and the correct vector and sum should be 
 *  returned by the functions
 *  
 *  One bug that I have is that the base destructor is not being called
 *  warning!! this function will cause memory leaks
 */
int main()
{
    int array[]={1,2,3,4,5,6,7,8,9,10};
    Number *even=new Even(array,sizeof(array)/sizeof(array[0]));
    Number *odd=new Odd(array,sizeof(array)/sizeof(array[0]));
    vector<int> temp=filter(even);
    cout<<"The even numbers in array are: ";
    for(unsigned int i=0; i<temp.size();i++){
        cout<<temp[i]<<" ";
    }
    cout<<endl;
    cout<<"The sum of all the even numbers is "
        <<addAll(even)<<endl;
    temp=filter(odd);
    cout<<"The odd numbers in array are: ";
    for(unsigned int i=0; i<temp.size();i++){
        cout<<temp[i]<<" ";
    }
    cout<<endl;
    cout<<"The sum of all the odd numbers is "
        <<addAll(odd)<<endl;
    delete even;
    delete odd;
}

不是直接回答你的问题,但这是非常错误的:

Number(int *a, int i):
    arr(a),n(i){}
virtual ~Number(){ delete [] arr;
    cout<<"In Number destructor"<<endl;
}

您没有将arr设置为指向动态分配的内存,因此不要期望从析构函数调用delete[] arr时能够正确工作。事实上,它很可能会调用内存访问冲突,从而突然终止程序(再考虑一下,也许这是对您的问题的直接回答)。

虚析构函数是一种转移注意力的方法,实际上基类析构函数正在被调用,这就是您看到错误的原因。请注意,虚析构函数的目的是确保在通过基类指针删除派生对象时调用派生类析构函数。

正在发生的事情是,你正在传递array,一个指向堆栈上数组的指针,给Number构造函数,它存储它,然后愉快地尝试删除它(两次,甚至!)当它的析构函数被调用。堆栈数组不需要,也不应该是 deleted,更不用说两次了。

你的基类析构函数被调用。但它的核心转储在:

delete [] arr;

,这是因为你试图删除array内存,它是在堆栈上分配的,而不是在堆上。

你的程序还有很多其他的问题