四、Object-Based Programming
1. Big Three
Big Three是指当类中有指针对象时,class除了构造函数constructor之外还要提供copy constructor, copy operator, destructor,以矩阵类为例:
class Matrix
{
public:
Matrix(int row, int col):_row(row), _col(col)
{
_pmat = new double[row * col];
}
Matrix(const Matrix& rhs);
Matrix& operator= (const Matrix& rhs);
~Matrix()
{
delete[] _pmat;
}
private:
int _row, _col;
double* _pmat;
}
Matrix::Matrix(const Matrix& rhs):_row(rhs._row), _col(rhs._col)
{
int elem_cnt = _row * _col;
_pmat = new double[elem_cnt];
for(int ix = 0; ix < elem_cnt; ++ix)
_pmax[ix]