I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.
To learn that, I have made the following simple code. It c
I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.
It is not completely true. When you have pointers in your class and allocate the memory using new
then you have to worry about copy constructor. Also, don't forget the assignment operator and destructor.
You have to delete the memory allocated using delete
.
It's called Law Of The Big Three.
Example:
~Matrix(); //Destructor
Matrix(const Matrix& m); //Copy constructor
Matrix& operator= (const Matrix& m); //Assignment operator