Copy constructor with pointers

前端 未结 9 1111
清歌不尽
清歌不尽 2020-12-14 10:08

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

9条回答
  •  既然无缘
    2020-12-14 10:21

    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
    

提交回复
热议问题