我的虚析构函数不会在抽象类中调用
My virtual destructor will not be called in a abstract class
我的程序不会调用基类的虚析构函数。我发现了这个方法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
构造函数,它存储它,然后愉快地尝试删除它(两次,甚至!)当它的析构函数被调用。堆栈数组不需要,也不应该是 delete
d,更不用说两次了。
你的基类析构函数被调用。但它的核心转储在:
delete [] arr;
,这是因为你试图删除array
内存,它是在堆栈上分配的,而不是在堆上。
你的程序还有很多其他的问题
相关文章:
- 尝试在 std::map 中插入抽象类时没有用于调用的匹配函数
- C++ abort() 在函数内的抽象类对象指针调用上
- C++ 抽象类构造函数调用
- 从子类中的抽象类调用方法
- 使用抽象指针调用派生类函数
- 无法从抽象类调用按钮信号
- 从派生类调用的抽象基类成员函数
- 抽象基类调用父级的纯虚函数
- 调用抽象类的析构函数
- 安卓系统上的Qt蓝牙.通过抽象类上的jni调用java类
- 如何重写调用另一个抽象类的抽象类的虚拟函数
- 我可以从子类以外的另一个类调用抽象基类的公共赋值运算符吗?
- 如何在抽象类中调用和使用复制构造函数
- 如何从基类调用重载函数(非抽象)
- C++抽象基类调用自己的纯虚函数会导致"Undefined reference"
- 从抽象基类调用派生类的 c'tor
- 通过抽象基类调用未知派生类的复制构造函数
- 在抽象类构造函数中,为什么需要调用永远不会被调用的虚基类的构造函数?< / h1 >
- 如果函数是从抽象类派生的,则不能用具体的类指针调用该函数
- 在抽象类中调用抽象方法