在实现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.相关推荐

  1. Effective C++ 读书笔记之Part2.Constructors, Destructors, and Assignment Operators

    5.Know what functions C++ silently writes and calls. 总结:编译器可以暗自为class创建default构造函数.copy构造函数.copy ass ...

  2. Effective C++ 改善55个方法

    美·Scott Meyers 候捷 电子工业 2011 刚才看到个会议时间有点晚,3.25论文都提交了 谷歌去广告的插件, 最后投了这个会议,刚刚好正合适.我说金钱与时间 ACCUSTOMING YO ...

  3. Effective C++读书笔记 摘自 pandawuwyj的专栏

    Effective C++读书笔记(0)       Start   声明式(Declaration):告诉编译器某个东西的名称和类型,但略去细节.   std::size_t numDigits(i ...

  4. Effective C++知识点

    条款01:视C++为一个语言联邦(View C++ as a federation of languages.) 对于内置类型而言,pass-by-value通常比pass-by-reference高 ...

  5. Effective C++:改善程序与设计的55个具体做法

    Effective C++:改善程序与设计的55个具体做法 二.构造/析构/赋值运算 05 Know what functions C++ silently writes and calls. 06 ...

  6. 《Effective C++》》阅读笔记

    第一章    Accustoming Yourself to C++ 1           View C++ as a federation of language (1).             ...

  7. Effective C++改善程序与设计的55个具体做法笔记

    Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...

  8. 笔记: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 ...

  9. Xcode @property attributes (nonatomic, copy, st...

    2019独角兽企业重金招聘Python工程师标准>>> Nonatomic nonatomic is used for multi threading purposes. If we ...

最新文章

  1. apppath php,App.php
  2. unity windows打包ios_ios打包unity应用以及配置签名!
  3. 裁剪Linux小系统
  4. Redis 修改密码
  5. POJ-3352-RoadConstruction(边双联通分量,缩点)
  6. Android开发之android与JS互调 | Android与H5互调(附源码)
  7. PDC Party 即将在东莞登场
  8. 算法设计与分析——递归与分治策略——快速排序
  9. protobuf导出golang,调整默认tag的方法
  10. SCCM2007系列教程之八资产管理
  11. 《Adobe Photoshop CS4中文版经典教程》—第1课1.7节检查更新
  12. 延禧宫有刺客!快把他收了!
  13. 2018年内蒙古孕产妇、婴儿死亡率实现双下降
  14. python的文本编辑geny_android模拟器(genymotion)+appium+python 框架执行基本原理(目前公司自己写的)...
  15. list 分页_mybatis一对多分页查询
  16. python元组是有序还是无序_python-03 元组和字典
  17. Android Verified Boot
  18. VC6.0中使用64位整型
  19. CentOS7配置阿里源
  20. 虚拟机服务器异常怎么解决,windows server 2016 虚拟机异常关闭

热门文章

  1. java 爬虫(爬新浪新闻) 如何从零开始 - 简书
  2. 中学计算机课 打字教案,计算机课教案.doc
  3. 用Whats Up来监控系统服务
  4. matlab的协方差计算函数cov
  5. 微软Windows11 21H2 22000.51 X64 官方正版ISO镜像系统
  6. day14,匿名函数,排序,筛选,映射,递归,二分法
  7. 基于FPGA的时序分析
  8. Android如何支持多种屏幕
  9. Flash 透明播放
  10. 好用免费的视频去水印工具软件,去水印微信小程序,操作简单,方便快捷,教你视频怎么水印