I'm trying to write a class that contains a double* array which can be filled via various ways, at the end of the program, the memory should be deallocated - alas, this does not work.
I get the notification that "programname.exe has triggered a breakpoint", which leads me to the last line of my main.cpp - when I delete my destructor it works fine, so I'm assuming it has got to do something with that.
Here's the relevant code:
.h
#pragma once
using namespace std;
#include <iostream>
class polynom
{
public:
polynom(int grad, double* arr);
polynom(int grad);
polynom();
~polynom(void);
polynom& operator=(polynom p);
friend ostream& operator<<(ostream& os, const polynom& p);
private:
int grad;
double* arr;
};
.cpp
polynom::polynom(int grad, double* arr)
{
this->grad = grad;
this->arr = arr;
}
polynom::polynom(int grad)
{
this->grad = grad;
this->arr = new double[grad];
}
polynom::polynom()
{
arr = NULL;
}
polynom::~polynom()
{
delete[] arr;
}
main
void main()
{
double arr1[] = {5,0,1};
double arr2[] = {3,2,1};
polynom p1 = polynom(2, arr1);
polynom p2 = polynom(2, arr2);
system("pause");
}
Thanks a lot!