Let's consider what would happen if you don't disable the default copy constructor and assignment operators: they are automatically implemented as a shallow copy.
This means that the values of fields in your class are copied. For pointers, it means the pointers are copied, not duplicated (the memory address itself is copied, not the pointed value), i.e. both the original and the copy will point to the same memory address. When the destructor tries to free the resources (delete) it will do it twice, the second causing a runtime error of double freeing the resource.
In the below example m_resouirce will be double freed.
#include <iostream>
class DoubleArrayRAII final
{
private:
double * m_resource;
public:
DoubleArrayRAII(size_t size) : m_resource { new double[size] } { }
~DoubleArrayRAII()
{
std::cout << "Freeing memory..." << std::endl;
delete[] m_resource;
}
// Delete copy constructor and assignment operator
//DoubleArrayRAII(const DoubleArrayRAII&) = delete;
//DoubleArrayRAII& operator=(const DoubleArrayRAII&) = delete;
};
int main()
{
DoubleArrayRAII a(5);
DoubleArrayRAII b = a; // Double free when released
DoubleArrayRAII c(a); // Double free when released
return 0;
}
I hope this helps.