The Big Three in C++
Constructor, destructors and assignment operators There is a rule of thumb in C++ that if a class defines a destructor, constructor and copy assignment operator - then it should explicitly define these and not rely on their default implementation. Why do we need them? In the example below, the absence of an explicit copy constructor will simply make an exact copy of the class and you end up with two classes pointing to the same memory address - not what you want. When you delete the array pointer in one class for example, the other class will be pointing to memory to which you have no idea as what it contains. Consider the following example class which is used to house an array of integers plus its size. It is written in such a way as to guarantee a crash: [code language="cpp"] #include ,algorithm> class Array { private: int size; int* vals; public: ~Array(); Array( int s, int* v ); }; Array::~Array() { delete vals; vals = NULL; ...