2

I hope I'm phrasing my self well, let me explain.

My hirarchey :

   Shape
Circle  Quad
        Square

And another class , allShapes , which contains a dynamic array of pointers to Shapes. (Shape **) and its size.

I'm trying to implement + operator, which suppose to create a new allShapes object, by creating a new Shape ** array from the elements stored inside the two objects that initiated the operator. Here is the function:

allShapes allShapes::operator+(const allShapes & other) const
{
    allShapes newAllShapes;
    newAllShapes._arr = new Shape *[_size + other._size];
    newAllShapes._size = _size + other._size;
    int i;
    for (i = 0; i < _size; i++)
        newAllShapes._arr[i] = _arr[i];
    for (int j = 0; j < other._size; j++, i++)
        newAllShapes._arr[i] = other._arr[j];
    return newAllShapes;
}

I initially tryed this but this result in an error in my destructor, because I'm only copying the addresses I guess it tries to delete the same object twice..

I tryed using the Shape CConstructor , by replacing the lines with this:

newAllShapes._arr[i] = new Shape(*_arr[i]);

My CConstructor:

Shape::Shape(const Shape & other)
{
    _totalNumOfShapes++;
    _shapeName = other._shapeName;
    _centerPoint = other._centerPoint;
}

But it result in an error

"Object of abstract class are not allowed" .

How can I solve this? Hope I included everything needed.

sagi
  • 40,026
  • 6
  • 59
  • 84
  • Not your question, but: use `std::vector` for arrays. – Cheers and hth. - Alf May 10 '18 at 19:43
  • Its a school assignment.. shouldn't implement something we haven't learned yet.. @Cheersandhth.-Alf – sagi May 10 '18 at 19:44
  • 1
    You will need some form of `clone` pattern, a `virtual` member function that returns a new copy of the object it's called on. – François Andrieux May 10 '18 at 19:44
  • 2
    You need to give the objects in your heirarchy a virtual `clone` function that creates new objects of the types, and use that to populate your array. You also need to think hard about if you really need to do this at all. –  May 10 '18 at 19:45

0 Answers0