Greetings. I define a class, D, one of whose members is a pointer to a class, E. The class E has two subclasses, E1 and E2. When I run the first code shown below, in which a D variable is declared and initialized in different statements, I get a segmentation fault. When I run the second code shown below, in which a D variable is declared and initialized in statement, I get the correct behavior. Can someone please tell me why, and how I can change the first code so that the D variable is declared and initialized in different statements but still runs correctly? Thanks.
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
using std::string;
using std::vector;
class E {
public:
E() {}
virtual ~E() {}
virtualvoid Process_x(const vector<int>& n) = 0;
protected:
void Process_c_x(const vector<int>& nc) {}
};
class E1 : public E {
public:
E1() {}
~E1() {}
void Process_x(const vector<int>& nc)
{
E::Process_c_x(nc);
}
};
class E2 : public E {
public:
E2() {}
~E2() {}
void Process_x(const vector<int>& nc)
{
E::Process_c_x(nc);
}
};
class D {
public:
D(const vector<int>& a, int b)
{
if (b == 1)
p = new E1();
else
p = new E2();
}
D() {}
~D() {delete p;}
void Process_x(const vector<int>& n)
{
p->Process_x(n);
}
private:
E *p;
};
int main(int argc, char **argv)
{
D d;
vector<int> a;
a.push_back(1);
int b = 2;
d = D(a, b);
}
Second code (only main program shown; all other code is the same)
1 2 3 4 5 6 7 8 9 10 11
...
int main(int argc, char **argv)
{
vector<int> a;
a.push_back(1);
int b = 2;
D d(a, b);
}
You should better initialize all attributes of your object to avoid accessing some undefined value like in your destructor. D() : p() {}
and better have a look at p before trying to delete a null pointer.
@OP: rule of three. If you need to code a destructor, a copy constructor or an assignment operator, you probably need the three.
You are trying to delete the same memory twice
> look at p before trying to delete a null pointer.
unnecessary. deleting a null pointer does nothing
Line 78 copies D(a, b) to d. Now you've two objects (D(a, b) and d) referencing the same E. At first D(a, b) was destroyed due to its temporary nature which in turn destroys the E. Finally when leaving main, d will be destroyed deleting E another time.