博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 拷贝控制和继承
阅读量:4087 次
发布时间:2019-05-25

本文共 2770 字,大约阅读时间需要 9 分钟。

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*/ }

}

 

 

转载地址:http://syyii.baihongyu.com/

你可能感兴趣的文章
【JAVA数据结构】先进先出队列
查看>>
String类的intern方法随笔
查看>>
【泛型】一个简易的对象间转换的工具类(DO转VO)
查看>>
1.随机函数,计算机运行的基石
查看>>
MouseEvent的e.stageX是Number型,可见as3作者的考虑
查看>>
移植Vim配色方案到Eclipse
查看>>
从超链接调用ActionScript
查看>>
谈谈加密和混淆吧[转]
查看>>
TCP的几个状态对于我们分析所起的作用SYN, FIN, ACK, PSH,
查看>>
网络游戏客户端的日志输出
查看>>
关于按钮的mouseOver和rollOver
查看>>
Netty框架
查看>>
Socket经验记录
查看>>
对RTMP视频流进行BitmapData.draw()出错的解决办法
查看>>
FMS 客户端带宽计算、带宽限制
查看>>
在线视频聊天(客服)系统开发那点事儿
查看>>
SecurityError Error 2148 SWF 不能访问本地资源
查看>>
Qt 静态编译后的exe太大, 可以这样压缩.
查看>>
3D游戏常用技巧Normal Mapping (法线贴图)原理解析——基础篇
查看>>
乘法逆元
查看>>