Both your estimate
class's constructor and destructor seem fine (you dynamically allocate memory with new[]
in the constructor, and delete[]
it in the destructor).
But I think you may have problems during copies.
In fact, your class has default copy constructor and operator=
, but the default behavior is not good in this case; in fact, the default behavior is just member-wise copy, but copying the raw pointer tHanning
data member is a "leaktrocity".
Either disable copy constructor and operator=
(e.g. declaring them private
in C++98/03, or using the new = delete
C++11 syntax), or give a proper implementation for them.
Proper implementation should do deep copy of the owned array (with proper deletion of any previously allocated array).
I think the best thing you can do is to use std::vector<double>
instead of the raw pointer.
If you are really such highly constrained on the (relatively small) overhead of std::vector
, then consider using a smart pointer like std::unique_ptr
:
#include <memory> // for std::unique_ptr
class estimate
{
private:
std::unique_ptr<double[]> tHanning; // <--- SMART pointer
public:
estimate()
: tHanning(new double[1000])
{
for (int m = 0; m < 1000; m++)
{
tHanning[m] = (0.5-0.5*cos(2.0*PI*(m+1)/(1001)));
}
}
// No need to define destructor - smart pointer automatically deletes its owned stuff
};
In this way, the class will be movable but not copyable, and you will incur almost zero overhead if compared to the raw pointer case.
As a bouns reading, consider the Rule of Three in C++.
Again, in your HBMain
class, don't use a raw estimate *objEstimate
pointer data member.
Instead, use smart pointers like:
class HBMain
{
....
private:
std::unique_ptr<estimate> objEstimate;
// Again, no need to define a custom destructor for deleting objEstimate.
// std::unique_ptr will do proper deletion *automatically*.
};