I have the code:
class Vector3D : public Vector{
protected:
Point3D * start;
Point3D * end;
public:
~Vector3D(){delete start; delete end;}
Vector3D(Point3D * start, Point3D * endOrLength, bool fromLength){
this->start = start;
if(fromLength){
this->end = new Vector3D(*start+*endOrLength); //the operator '+' is defined but I won't put it here,
//because it's not important now
}else
this->end = endOrLength;
}
Point3D * getStart(){return start;}
Point3D * getEnd(){return end;}
};
Now, I have the code:
Vector3D v(new Point3D(1,2,3), new Point3D(2,3,4), 0); //FIRST CREATION
Vector3D v(new Point3D(1,2,3), new Point3D(1,1,1), 1); //SECOND CREATION
First and Second creation give me the same Vector3D, but I think it may produce memory leaks.
It’s a true? And how to solve it? I guess it’s not elegant to make it that way:
...
if(fromLength){
this->end = new Vector3D(*start+*endOrLength);
delete endOrLength;
}else
...
Maybe it’s better to put const Point3D &endOrLenght, I don’t know what would be a good mannier? The same with getStart/getEnd – should it return pointer:
Point3D * getStart(){return start;}
or just the variable:
Point3D getStart()(return *start)
?
You code here may not be the best way of handling this, but to directly fix your problem:
I think a better solution to your problem is to use smart pointers, and the best solution is to see if you can replace pointers.