need help with destructor
I have a class named PolyLine with the following data members:
double *xArray;
double *yArray;
int length;
I have a destructor:
~PolyLine()
{
cout << "I'm destroying you";
delete [] xArray;
delete [] yArray;
}
when I delete a PolyLine:
delete p1;//where p1 is a pointer to a PolyLine
The text "I'm destroying you" is printed but the object can still be printed. Could someone tell me what I am doing wrong ? Thanks
thanks guys... one more question...
I tried to print the PolyLine after the new and improved destructor and I can honestly say that I've never been to happy to see a segmentation fault =)
Could you guys answer another question for me ?
PolyLine *p1 = new PolyLine(x, y, 3); //dynamic constructor
PolyLine *p2 (p1);//copy constructor
I want the second line to look like PolyLine p2(p1); ie: I want p2 to be a reference not a pointer. I want p2 to be constructed with the following method (can be modified if need be).
btw: datamembers for PolyLine are:
private:
double *xArray;
double *yArray;
int length;
PolyLine(const PolyLine& src)
{
int i;
double x1, x2;
cout << "inside deep copy\n";
for(i=0;i<src.length;i++)
{
x1 = src.xArray[i];
xArray[i] = x1;
x2 = src.yArray[i];
yArray[i] = x2;
}
length = src.length;
}
I am not sure what happens when:
PolyLine *p2 (p1);//copy constructor
is executed.. p2 points at p1 and when I destroy p1, p2 gets destroyed too but I don't know what code is making p2 point at p1. Is this something built in to C++? I don't think it's any of my methods doing it. This has had me stumpped for some time.
I have tried changing the statement to this:
PolyLine &p2 (p1);//copy constructor
and I got this error:
conversion from `PolyLine *' to non-scalar type `PolyLine' requested
a1.cc:14: cannot initialize `PolyLine &' from `PolyLine *'
and I tried changing the statement to this:
PolyLine p2 (p1);//copy constructor
and got the following error:
no matching function for call to `PolyLine::PolyLine (PolyLine *&)'
PolyLine.h:10: candidates are: PolyLine::PolyLine(double *, double *, int)
PolyLine.h:24: PolyLine::PolyLine(const PolyLine &)
Anyone know what I'm doing wrong ?
Thanks