条款16:在operator =中对所有数据成员赋值

来源:互联网 发布:linux查看线程cpu 编辑:程序博客网 时间:2024/05/22 03:18
class Base
{
public:
        Base(int initialvalue = 0):x(initialvalue){}
private:
        int x;
};

class Derived:public Base
{
public:
        Derived(int initialvalue):Base(initialvalue),y(initialvalue){}
        Derived& operator=(const Derived& rhs)
        {
                if(this == &rhs)
                        return *this;
                Base::operator=(rhs);
                y = rhs.y;
                return *this;
        }
private:
        int y;
};
原创粉丝点击