Constructors / Destructors

May 26, 2010 at 2:30pm
Hello, I'm having some problems creating proper constructors/destructors for the following classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Nhood;

class Tuple
{
public:
	long	m_nValues[G_TUPLE_DIMENSION];
	long	m_nID;
	Nhood*  nhood;
};

class Nhood
{
public:
	bool isContainedBy(Nhood* n2)
	{...}
	void intersect(Nhood *n2)
	{...}
	void union_(Nhood *n2)
	{...}

	long	min[G_TUPLE_DIMENSION];
	long	max[G_TUPLE_DIMENSION];
	list<Tuple*> tuples;
};


I tried something like

1
2
3
4
5
6
7
8
9
Nhood(){}
~Nhood(){delete min;delete max;tuples.clear();}

Nhood(const Nhood &v)
{
	copy(v.tuples.begin(),v.tuples.begin(),std::back_inserter(tuples));
	for (int i=0;i<G_QI_DIMENSION;++i)
	{min[i]=v.min[i];max[i]=v.max[i];}
}

but my program just crashes as soon as I start it..can you please give me a hand?
thanks in advance!
Last edited on May 26, 2010 at 2:30pm
May 26, 2010 at 2:55pm
Ok.

1.) You do not use new to allocate min and max, so you don't need to delete them. You also don't need to clear your std::list.
2.) You for loop in your copy constructor seems to be running from 0 to G_QI_DIMENSION, but the arrays are only of length G_TUPLE_DIMENSION.
May 26, 2010 at 3:10pm
Thank you, the problem was caused by the destructor :)

however, by making the destructor as ~Nhood(){}, will the class elements be properly destructed when i use .clear() on a list / vector of Nhood elements?
May 26, 2010 at 3:52pm
If it is a vector of *just* Nhood:

std::vector<Nhood>

then yes. Otherwise, you will have to deal with whatever is inside there yourself.
May 26, 2010 at 4:09pm
Thank you
Topic archived. No new replies allowed.