我的程序不会调用基类的虚析构函数。我找到了这个方法 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;
}
答案 0 :(得分:0)
不是你问题的直接答案,但这是非常错误的:
Number(int *a, int i):
arr(a),n(i){}
virtual ~Number(){ delete [] arr;
cout<<"In Number destructor"<<endl;
}
您没有将arr
设置为指向动态分配的内存,因此从析构函数调用时不要指望delete[] arr
正常工作。事实上,它很可能会调用一个突然终止程序的内存访问冲突(第二个想法,也许它毕竟是你问题的直接答案)。
答案 1 :(得分:0)
虚拟析构函数是一个红色鲱鱼,事实上基类析构函数被调用,这就是你看到错误的原因。请注意,虚拟析构函数的要点是确保在通过基类指针删除派生对象时将调用派生的类析构函数。
发生的事情是你将array
(一个指向堆栈数组的指针)传递给Number
构造函数,后者存储它然后很高兴尝试删除它(两次,甚至!)当它的析构函数被调用时。堆栈数组不需要,也不应该是delete
d,更不用说两次了。
答案 2 :(得分:0)
你的基类析构函数会被调用。但它的核心转储在:
delete [] arr;
那是因为你试图删除在堆栈上而不是在堆上分配的array
内存。
你的程序还有很多其他问题。