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*/ }
}
- C++ 拷贝控制和继承
- 【足迹C++primer】54、继承类的范围,构造函数和拷贝控制
- C++primer学习:面象对象程序设计(4):继承类中的作用域和拷贝控制
- 合成拷贝控制与继承
- Day43、向下造型、继承方式和访问控制属性、子类的拷贝构造和拷贝赋值、多重继承
- 【足迹C++primer】43、拷贝控制和资源管理
- c++primer——拷贝控制和运算符重载
- c++primer要点-拷贝控制
- 《C++ Primer》读书笔记第十五章-3-构造函数和拷贝控制、容器与继承
- 【足迹C++primer】53、访问控制和继承
- 面向对象程序设计之访问控制、继承与拷贝控制
- 复制控制和继承
- 复制控制和继承
- C++ 拷贝控制和资源管理
- 拷贝控制和引用计数
- 拷贝控制和对象移动
- 【足迹C++primer】45、拷贝控制示例
- C++primer学习:拷贝控制(1)
- 编写中,未完
- C语言学习笔记(十) -指针下 Pointer
- PAT1093
- Android 使用百度地图SDK 定位和显示基础地图常见错误 locationclient.start()和mapview is null等等
- Valid Palindrome
- C++ 拷贝控制和继承
- 关于block的那些事
- iOS ViewController生命周期
- Linux进程关于文件描述符的数量限制
- spark实现hive的合并输入很多小文件为指定大小的大文件的优化功能
- std::numeric_limits<float>::max() warning C4003: “max”宏的实参不足
- 概率论--条件概率与乘法公式
- hdu4352 XHXJ's LIS 数位DP+状态压缩
- Mongodb学习笔记五、查询操作