Copy all parts of an object.
在实现Derived类的copy constructor和copy assignment的时候,不仅要copy derived类的成员,还要拷贝base类的成员。
#include <string>
#include <iostream>class Animal
{public:Animal(const std::string & name, int age) : m_name(name),m_age(age){std::cout << "Animal ctor called." << std::endl;}
protected:std::string m_name;int m_age;
public:Animal(const Animal &animal) {m_name = animal.m_name;m_age = animal.m_age;}Animal & operator=(const Animal &animal) {m_name = animal.m_name;m_age = animal.m_age;return *this;}
};class Dog : private Animal
{public:Dog(const std::string &name, int age, const std::string &color) : Animal(name, age), m_color(color) {std::cout << "Dog ctor called." << std::endl;}Dog(const Dog &dog) : Animal(dog.m_name, dog.m_age) {m_color = dog.m_color;}Dog &operator=(const Dog &dog) {Animal::operator=(dog);m_color = dog.m_color;return *this;}
protected:std::string m_color;
};int main() {Animal aanimal("dog", 9);Dog adog("huabao", 5, "red");Dog bdog("xinba", 6, "yellow");adog = bdog; Dog cdog(adog);return 0;
}
由于拷贝对象是每一个成分都需要拷贝,即derived类实现copy ctor和copy assignment时需要调用base类的copy ctor和copy assignment,因此如果想禁用(derived)类的copy函数,只需要将base类的copy函数禁用即可,可将base类的copy ctor和copy assignment设置成private(只需声明,不需要实现)即可。也可使用delete实现。
#include <string>
#include <iostream>class Animal
{public:Animal(const std::string & name, int age) : m_name(name),m_age(age){std::cout << "Animal ctor called." << std::endl;}
protected:std::string m_name;int m_age;
private:Animal(const Animal &animal);Animal & operator=(const Animal &animal);
};class Dog : private Animal
{public:Dog(const std::string &name, int age, const std::string &color) : Animal(name, age), m_color(color) {std::cout << "Dog ctor called." << std::endl;}
#if 0 // 由于base类禁用了copy函数,因此这里无法实现Dog(const Dog &dog) : Animal(dog) {m_color = dog.m_color;}Dog &operator=(const Dog &dog) {Animal::operator=(dog);m_color = dog.m_color;return *this;}
#endif
protected:std::string m_color;
};int main() {Animal aanimal("dog", 9);Dog adog("huabao", 5, "red");Dog bdog("xinba", 6, "yellow");
#if 0 // 非法调用adog = bdog; Dog cdog(adog);
#endifreturn 0;
}
Copy all parts of an object.相关推荐
- Effective C++ 读书笔记之Part2.Constructors, Destructors, and Assignment Operators
5.Know what functions C++ silently writes and calls. 总结:编译器可以暗自为class创建default构造函数.copy构造函数.copy ass ...
- Effective C++ 改善55个方法
美·Scott Meyers 候捷 电子工业 2011 刚才看到个会议时间有点晚,3.25论文都提交了 谷歌去广告的插件, 最后投了这个会议,刚刚好正合适.我说金钱与时间 ACCUSTOMING YO ...
- Effective C++读书笔记 摘自 pandawuwyj的专栏
Effective C++读书笔记(0) Start 声明式(Declaration):告诉编译器某个东西的名称和类型,但略去细节. std::size_t numDigits(i ...
- Effective C++知识点
条款01:视C++为一个语言联邦(View C++ as a federation of languages.) 对于内置类型而言,pass-by-value通常比pass-by-reference高 ...
- Effective C++:改善程序与设计的55个具体做法
Effective C++:改善程序与设计的55个具体做法 二.构造/析构/赋值运算 05 Know what functions C++ silently writes and calls. 06 ...
- 《Effective C++》》阅读笔记
第一章 Accustoming Yourself to C++ 1 View C++ as a federation of language (1). ...
- Effective C++改善程序与设计的55个具体做法笔记
Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...
- 笔记:A Novel Representation of Parts for Accurate 3D Object Detection and Tracking in Monocular Images
A Novel Representation of Parts for Accurate 3D Object Detection and Tracking in Monocular Images 20 ...
- Xcode @property attributes (nonatomic, copy, st...
2019独角兽企业重金招聘Python工程师标准>>> Nonatomic nonatomic is used for multi threading purposes. If we ...
最新文章
- apppath php,App.php
- unity windows打包ios_ios打包unity应用以及配置签名!
- 裁剪Linux小系统
- Redis 修改密码
- POJ-3352-RoadConstruction(边双联通分量,缩点)
- Android开发之android与JS互调 | Android与H5互调(附源码)
- PDC Party 即将在东莞登场
- 算法设计与分析——递归与分治策略——快速排序
- protobuf导出golang,调整默认tag的方法
- SCCM2007系列教程之八资产管理
- 《Adobe Photoshop CS4中文版经典教程》—第1课1.7节检查更新
- 延禧宫有刺客!快把他收了!
- 2018年内蒙古孕产妇、婴儿死亡率实现双下降
- python的文本编辑geny_android模拟器(genymotion)+appium+python 框架执行基本原理(目前公司自己写的)...
- list 分页_mybatis一对多分页查询
- python元组是有序还是无序_python-03 元组和字典
- Android Verified Boot
- VC6.0中使用64位整型
- CentOS7配置阿里源
- 虚拟机服务器异常怎么解决,windows server 2016 虚拟机异常关闭