把"Essencial C++"读薄(二)

本文探讨了C++中的Object-Based Programming,重点介绍了Big Three(构造函数、复制构造函数、析构函数),mutable和const的使用,以及friend和仿函数(function-like classes)的概念,强调了在类设计中的重要原则。
摘要由CSDN通过智能技术生成

四、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] 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值