0

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?

Opal Koren
  • 51
  • 1
  • 7
  • 3
    It sounds like a violation of the [rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three). – François Andrieux Aug 20 '18 at 19:21
  • Setting pointers to `NULL` after you `delete` them in the destructor is completely pointless. – Jesper Juhl Aug 20 '18 at 19:21
  • Have you declared your own copy constructor? If not, you probably have shallow copies that are sharing pointed-to member data and deleting it early – alter_igel Aug 20 '18 at 19:23
  • @JesperJuhl Not necessarily completely useless. It helps keep the habit of nulling deleted pointers which can be helpful when the pointer isn't about to be destroyed. And they are likely to be optimized out anyway. Though `nullptr` is preferred to `NULL` and using proper containers or smart pointers makes the problem moot. – François Andrieux Aug 20 '18 at 19:23
  • Seems like you might be overusing dynamic allocation. Using value semantics would greatly simplify your system, if you can. – François Andrieux Aug 20 '18 at 19:25
  • @FrançoisAndrieux out of the Big 3 Linux compilers (gcc, icc, clang) only gcc was able to optimize away the move. Both icc and clang (latest version) dutifully moved 0 and broke TCO. Snippet: https://gcc.godbolt.org/z/nl8KgN So I urge you to reconsider your 'likely' statement. – SergeyA Aug 20 '18 at 19:27
  • I just finished to read what u just sent, but yet i dont know how to fix the problem? can u give me example to how i fix it please? – Opal Koren Aug 20 '18 at 19:39
  • @OpalKoren Any [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) is going to explain the rule of three. You really can't be a competent C++ programmer until you understand it. – john Aug 20 '18 at 21:10

0 Answers0