0

I have class with move constructor. After moving, pointer became null. Do I have to check for not_null in destructor before calling delete?

class A {
    int *data;
public:
    A(size_t size) : data(new int[size]) {}

    A(A &&rhs) : data(rhs.data) {
        rhs.data = nullptr;
    }

    ~A() {
        if (data) {
            delete [] data;
        }
        //or
        delete [] data;
    }
}
Peter
  • 375
  • 2
  • 11

1 Answers1

3

No, both delete and delete[] are well-defined for nullptr - they will do nothing.

Vittorio Romeo
  • 86,944
  • 30
  • 238
  • 393