I have this code :
class allShapes {
private:
Shape ** _arr;
int _size;
public:
allShapes();
~allShapes();
int getSize() const;
void addShape(Shape * newShape);
allShapes operator+(const allShapes & other) const;
}
allShapes::~allShapes()
{
if (_arr != NULL)
{
for (int i = 0; i < getSize(); i++)
{
delete _arr[i];
_arr[i] = NULL;
}
delete[] _arr;
_arr = NULL;
}
};
allShapes allShapes::operator+(const allShapes & other) const
{
allShapes newallShapes;
for (int i = 0; i < getSize(); i++)
{
newallShapes.addShape(_arr[i]);
}
for (int i = 0; i < other.getSize(); i++)
{
newallShapes.addShape(other._arr[i]);
}
return newallShapes;
}
My Main:
allShapes shapes;
Circle * c1 = new Circle(3, "myCircle");
Circle * c2 = new Circle(2, "yourCircle");
shapes.addShape(c1);
shapes.addShape(c2);
allShapes newshape = shapes + shapes;
I have a problem with my project:
a little of background: I have allShapes, that holds some other classes like Circle and Square and such..
I have a problem with my operator+,
it creates GOOD the newallShapes in the method, but then it in the last line: return newallShapes; it calls the destructor and then it deletes the pointers of the newallShapes and ruins everything!
how could i send the back to the main my new "newallShape" with all the pointers?