Class destructor being called twice after initialization

52 Views Asked by At

I have a class:

class square
{
    int* _ptr;
public:
    square(int * ptr )
    {
        _ptr = ptr;
        *_ptr = 0;
    }

    ~square()
    {
        delete _ptr;
    }
};

I have another class with a square member, and I initialize is like this:

member = Square::Square(new int);

When the new square created by the constructor goes out of scope, the destructor gets called and deletes the data at _ptr. Then when the program ends and the member gets destroyed, it called the destructor again and tries to delete the already deleted data. How can I fix this issue?

0

There are 0 best solutions below