Constructor calling constructor: SIGSEGV
I have a class with two constructors, one constructor has no arguments, the other constructor has one argument. The constructor with no arguments calls the other with a default argument. The issue is, that when I dynamically allocate memory with the first constructor, it does not remain set as control goes to the other constructor. I don't think i've made myself clear, so I will write some code to show you what I mean.
Code:
class Aclass {
public:
Aclass();
Aclass(int);
void test();
private:
int *var;
}
...
Aclass::Aclass()
{
Aclass(10);
}
Aclass::Aclass(int x)
{
var = new int[x]; // allocate memory
var[0] = x*x;
}
void Aclass::test()
{
// here var is NULL!
var[1] = 3; // SIGSEGV
// but if this object was created by calling Aclass(10) directly, there is no problem
}