C++ 拷贝控制和继承

来源:互联网 发布:js 微信支付接口 编辑:程序博客网 时间:2024/05/16 05:27

The synthesized operations copy, assign, or destroy the base-class part of the object along with the members of the derived part.

 

Whether a class needs to define the copy-control members depends entirely on the class' own direct members.A baseclass might define its own copy control while the derived uses the synthesize dversions or vice versa.

 

Classes with pointer members often need to define their own copy control to manage these members.

 

Note: If a derived class explicitly definesits own copy constructor or assignment operator, that definition completely overrides the defaults.The copy constructor and assignment operator for inherited classes are responsible for copying or assigning their base class components as well as the members in the class itself.

 

 

Defininga Derived Copy Constructor

If a derived class defines its own copyconstructor, that copy constructor usually should explicitly use the base-classcopy constructor to initialize the base part of the object:

class Base { /* ... */ };

class Derived: public Base {

public:

// Base::Base(const Base&) not invoked auto matically

Derived(const Derived& d):

Base(d) /* other member initialization*/ {/*... */ }

};

 

Derived-ClassAssignment Operator

As usual, the assignment operator is similar to the copy constructor:If the derived class defines its ownassignment operator, then that operator must assign the base part explicitly:

// Base::operator=(const Base&) notinvoked automatically

Derived &Derived::operator=(constDerived &rhs)

{

if (this != &rhs) {

Base::operator=(rhs); // assigns the basepart

// do whatever needed to clean up the oldvalue in the derived part

// assign the members from the derived

}

return *this;

}


Theassignment operator must, as always, guard against self-assignment. Assuming the leftand right-hand operands differ, then we call theBaseclass assignment operator to assign the base-class portion. That operator might bedefined by the class or it might be the synthesized assignment operator. It doesn't matterwecan call it directly. The base-class operator will free the old value in the base part of theleft-hand operand and will assign the new values from rhs. Once that operator finishes, we continuedoing whatever is needed to assign the members in the derived class.

 

Derived-Class Destructor

The destructor works differently from thecopy constructor and assignment operator: The derived destructor is never responsible for destroying the members of its base objects. The compiler always implicitly invokes the destructor for the basepart of a derived object. Each destructor does only what is necessary to clean up its own members:

class Derived: public Base {

public:

// Base::~Base invoked automatically

~Derived() { /* do what it takes to clean upderived members*/ }

}

 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝肠道蠕动慢怎么办 新生儿吃母乳吃不饱怎么办 小孩胃口不好怎么办呢 新生儿吃多了怎么办 宝宝奶量上不去怎么办 新生儿吃撑了怎么办 婴儿不够奶吃怎么办 产妇奶堵了怎么办 新生儿吃奶不吃奶粉怎么办 奶瓶吸奶费力怎么办 小孩上火感冒了怎么办 宝宝上火感冒了怎么办 3岁宝宝上火怎么办 上火又受凉感冒怎么办 上火引起的感冒怎么办 奶瓶排气孔漏水怎么办 奶嘴排气孔漏水怎么办 bbox吸管杯漏水怎么办 四个月宝宝拉肚子怎么办 租的房子坐月子怎么办 榨果汁不甜怎么办 宝宝不会喝奶粉怎么办 两个月宝宝不长肉怎么办 打疫苗后发烧怎么办 孕期不爱吃水果怎么办 孕期很少吃水果怎么办 三个月小孩不吃奶粉怎么办 三个月宝宝偏瘦怎么办 破壁机打果汁有沫怎么办 宝宝7个月坐不稳怎么办 婴儿头睡偏了怎么办天 宝宝不爱趴着怎么办 宝宝喜欢竖着抱怎么办 婴儿抱习惯了怎么办 新生儿总让抱着放下就哭可怎么办 三个月宝宝认人怎么办 三个月的宝宝认生怎么办 一岁半宝宝尿黄怎么办 一岁多宝宝尿少怎么办 1岁宝宝一晚没尿怎么办 抗利尿激素少怎么办