基于linux的c课件第.pptxVIP

  • 1
  • 0
  • 约1.82千字
  • 约 8页
  • 2021-09-29 发布于山西
  • 举报
■赋值构造与拷贝构造赋值也是构造拷贝、赋值与析构三位一体,一般同时出现缺省赋值构造与拷贝构造为浅拷贝如果对象没有指针成员,缺省行为即可满足要求,无需实现或重载这三个函数如果对象有指针成员,一般需要重载这三个函数■浅拷贝class A{public: A() : _n(0), _p(NULL) { } explicit A( int n ) : _n(n), _p(new int[n]) { } A( int n, int * p ) : _n(n), _p(p) { } A( const A that ) : _n(that._n), _p(that._p) { } A operator=( const A that ) { _n = that._n, _p = that._p; return *this; } virtual ~A() { if(_p){ delete[] _p, _p = NULL; } }public: int operator[]( int i ); const int operator[]( int i ) const;private: int _n; int * _p;};■浅拷贝int A::operator[]( int i ){ if( i 0 || i = 4 ) throw std::out_of_range( Out of ra

文档评论(0)

1亿VIP精品文档

相关文档