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
  • 435
  • 1
  • 3
  • 11

1 Answers1

4

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

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416