(c++常问问题三)拷贝构造函数和赋值构造函数拷贝基类

看下面代码

class base
{
public:
    base();
    base(const base& other)
    {
        //...
    }
    
    base& operator=(base& other)
    {
       //..
    }
};

class node : public base
{
public:
    node();
    //正确,通过初始化列表来调用基类拷贝构造
    node(const node& other)
      :base(other)
    {
        //...
    }
    
    //错误的示范...(赋值不应该重新构造基类)
    node& operator=(node& other):base(other)
    {
        //...
        return *this;
    }

    //正确,通过调用基类的赋值操作来实现
    node& operator=(node& other)
    {
        base::operator=(other);
        //..
        return *this;
    }
};

  

 

posted @ 2016-01-28 23:51  Jensen抹茶喵  阅读(547)  评论(0编辑  收藏  举报