I implemented a constructor that takes a string as argument, this causes the destructor to get called twice and then the program crashes (the program contains a raw pointer). I know that it has to do with the copy constructor that somehow gets called when this type of constructors are used. Below is a simple code that illustrates the problem.
I would appreciate your comments on how to fix the program to avoid it to crash. I need to use this kind of constructors. I would like to understand why the copy constructor gets called. I didn't do any explicit assignment.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class DebugClass
{
public:
DebugClass(void) {
data = NULL;
}
DebugClass(std::string str) {
data = new double[2];
data[0] = 1.0;
data[1] = 2.0;
}
DebugClass(DebugClass const& other) {
cout << "copy construction\n";
}
~DebugClass(void) {
if (data)
{
delete [] data;
data = NULL;
}
}
double* data;
};
int main()
{
DebugClass obj = DebugClass("Folder");
return 0;
}