继承的时候,子类的拷贝构造函数和重载运算符的实现
由[1] [2] 例子看出:子类中实现了拷贝构造和赋值函数,则调用子类的拷贝构造和赋值函数
[1]说明:
子类的拷贝构造中调用父类的拷贝构造;
子类的赋值函数中调用父类的赋值函数
#include<iostream>using namespace std;class Parent{public:Parent(){cout<<"Parent constru fun()"<<endl;}Parent(int a):m_a(a){cout<<"Parent constru fun(int )"<<endl;}Parent(const Parent&other):m_a(other.m_a){cout<<"Parent copy fun"<<endl;}Parent& operator= (const Parent& other){cout<<"Parent =fun"<<endl;if(this == &other)return *this;m_a = other.m_a;return *this;}int geta(){return m_a;}private:int m_a;};class Child : public Parent{public:Child(){cout<<"Child constru fun( )"<<endl;}Child(int a, int b):Parent(a),m_b(b){cout<<"Child constru fun(int, int)"<<endl;}Child(const Child& other) :Parent(other) //调用父类的拷贝构造函数{// Parent::Parent(other); //调用父类的拷贝构造函数(同上面的初始化列表)m_b = other.m_b;cout<<"Child copyfun"<<endl;}Child& operator= (const Child&other){cout<<"Child =fun"<<endl;Parent::operator=(other); //调用父类的重载赋值运算符if(this == &other)return *this;m_b = other.m_b;return *this;}int getb(){return m_b;}private:int m_b;};int main(){Child child1(2, 3);cout<<"========================================="<<endl;Child child2(child1);/*调用子类的拷贝构造,而子类的拷贝构造会先调用父类的拷贝构造函数*/cout<<"a ="<<child2.geta()<<",b ="<<child2.getb()<<endl;cout<<"========================================="<<endl;child2 = child1; /*调用子类的=函数,而子类的=函数会先调用父类的=函数*/cout<<"a ="<<child2.geta()<<",b ="<<child2.getb()<<endl;return 0;}
输出结果:
root@ubuntu:/home/ygm# ./mai
Parent constru fun(int )
Child constru fun(int, int)
=========================================
Parent copy fun
Child copyfun
a =2,b =3
=========================================
Child =fun
Parent =fun
a =2,b =3
[2]说明:
子类中拷贝构造函数未调用父类的拷贝构造函数;
子类的赋值号函数中未调用父类的赋值号函数;
#include <iostream>
using namespace std;
class Parent
{
public:Parent(){cout<<"Parent constru ()"<<endl;}Parent(int a):m_a(a){cout<<"Parent constru (int)"<<endl;}Parent(const Parent& other):m_a(other.m_a){cout<<"Parent copy constru"<<endl;}Parent& operator= (const Parent& other){cout<<"Parent = fun"<<endl;if(this == &other)return *this;m_a = other.m_a;return *this;}
private:int m_a;
};class Child : public Parent
{
public:Child(){cout<<"Child constru ()"<<endl;}Child(int a, int b):Parent(a), m_b(b){cout<<"Child constru (int, int)"<<endl;}//未调用父类的拷贝构造Child(const Child& other){m_b = other.m_b;cout<<"Child copy constru"<<endl;}//未调用父类的赋值函数Child& operator= (const Child& other){cout<<"Child = fun"<<endl;if(this == &other)return *this;m_b = other.m_b;return *this;}
private:int m_b;
};int main()
{Child child1(2, 3);cout<<"==================="<<endl;Child child2(child1);cout<<"==================="<<endl;child2 = child1;return 0;
}
输出:
root@ubuntu:/home/ygm# ./mai
Parent constru (int)
Child constru (int, int)
===================
Parent constru ()
Child copy constru
===================
Child = fun
[3]说明:
子类中没有实现拷贝构造和赋值函数:则采取默认的流程,
首先调用父类的拷贝构造函数。然后调用子类系统的拷贝构造函数。
首先调用父类的赋值重载函数。然后调用子类系统的赋值重载函数。
#include <iostream>
using namespace std;
class Parent
{
public:Parent(){cout<<"Parent constru ()"<<endl;}Parent(int a):m_a(a){cout<<"Parent constru (int)"<<endl;}Parent(const Parent& other):m_a(other.m_a){cout<<"Parent copy constru"<<endl;}Parent& operator= (const Parent& other){cout<<"Parent = fun"<<endl;if(this == &other)return *this;m_a = other.m_a;return *this;}
private:int m_a;
};class Child : public Parent
{
public:Child(){cout<<"Child constru ()"<<endl;}Child(int a, int b):Parent(a), m_b(b){cout<<"Child constru (int, int)"<<endl;}private:int m_b;
};int main()
{Child child1(2, 3);cout<<"==================="<<endl;Child child2(child1);cout<<"==================="<<endl;child2 = child1;return 0;
}
输出:
root@ubuntu:/home/ygm# ./mai
Parent constru (int)
Child constru (int, int)
===================
Parent copy constru
===================
Parent = fun
阳光梦总结:
[1]子类中没有实现拷贝构造和赋值函数:
默认的拷贝构造函数的行为:
首先调用父类的拷贝构造函数。然后为自己独有的各成员寻找拷贝构造函数。
如果这个类提供重写的拷贝构造函数,则调用重写的拷贝构造函数,如果成员的类提供的拷贝构造函数是private,编译将无法通过。;
如果这个类没有重写拷贝构造函数,则调用默认的拷贝构造函数;
默认的赋值运算的行为:
首先调用父类的赋值重载函数。然后为自己独有的各成员寻找赋值重载函数
。
如果这个类提供重写的赋值重载函数
,则调用重写的
赋值重载函数
,如果成员的类提供的
赋值重载函数
是private,编译将无法通过。;
如果这个类没有重写赋值重载函数,则调用默认的赋值重载函数;
[2]子类中实现了拷贝构造和赋值函数:
则调用子类的拷贝构造和赋值函数
继承的时候,子类的拷贝构造函数和重载运算符的实现相关推荐
- C++“拷贝构造函数”和“重载 = 运算符”
文章目录 一:拷贝构造函数和缺省拷贝构造函数 (一)拷贝构造函数 1.功能介绍 2.定义方法 3.特点 (二)缺省拷贝构造函数 1.功能 2.定义(系统自动生成) 3.特点 (三)"浅&qu ...
- 类的6个默认成员函数:构造函数、析构函数、拷贝构造函数、重载运算符、三/五法则
文章目录 6个默认成员函数 构造函数 概念 默认构造函数的类型 默认实参 概念 默认实参的使用 默认实参声明 全局变量作为默认实参 某些类不能依赖于编译器合成的默认构造函数 第一个原因 第二个原因 第 ...
- C++ 拷贝构造函数和重载赋值运算符的区别
文章目录 拷贝构造函数 重载赋值运算符 赋值运算符和拷贝构造函数最大区别是赋值运算符没有新的对象生成,而拷贝构造函数会生成新的对象. 为了更加形象 准确得描述 赋值运算符和拷贝构造函数得区别,将详细通 ...
- C++的拷贝构造函数、operator=运算符重载,深拷贝和浅拷贝、explicit关键字
1.在C++编码过程中,类的创建十分频繁. 简单的功能,当然不用考虑太多,但是从进一步深刻理解C++的内涵,类的结构和用法,编写更好的代码的角度去考虑,我们就需要用到标题所提到的这些内容. 最近,在看 ...
- C++ 类的深拷贝与浅拷贝||深拷贝通过重载拷贝构造函数与重载赋值运算符实现
http://blog.csdn.net/wangshihui512/article/details/9842225 在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作. 如果对象在申明的同 ...
- C++结构体:默认构造函数,复制构造函数,重载=运算符
新博客地址 www.virtclouds.com 原文地址 http://www.virtclouds.com/544.html C++结构体提供了比C结构体更多的功能,如默认构造函数,复制构造函数, ...
- 继承关系中的拷贝构造函数和赋值操作重载函数分析
文章目录 1 继承关系中的拷贝构造函数和赋值操作重载函数分析 1 继承关系中的拷贝构造函数和赋值操作重载函数分析 在继承关系中,如果子类未实现拷贝构造函数,那么在子类进行拷贝构造操作时,会直接调用父类 ...
- C++ 拷贝构造函数的在继承中的总结
目录 拷贝构造函数字面意思 拷贝构造函数基本格式: 下面代码证明不写拷贝构造函数,系统自动生成拷贝构造函数 子类继承父类的时候拷贝构造函数时,如果成员变量没有默认值,必须在初始化列表中把父类的构造函数 ...
- 什么是拷贝构造函数?拷贝构造函数何时被调用
1.什么是拷贝构造函数: CA(const CA& C)就是我们自定义的拷贝构造函数.可见,拷贝构造函数是一种特殊的构造函数,函数的名称必须和类名称一致,它的唯一的一个参数是本类型的一个引用变 ...
最新文章
- Celery--任务调度利器
- Java项目命名规范
- 重学ES6 函数的扩展(下)
- java spring异常处理_Java深入 - Spring 异常处理HandlerExceptionResolver
- Linux / pthread_create() 函数所使用的线程函数为什么必须是静态函数?
- SQL Server 堆heap 非聚集索引 Nonclustered index 行号键查找RID loopup结合执行计划过程详解
- 正则表达式验证密码强度
- 从招行数据架构调整,详解企业急需的数据中台与5大数字化转型
- Captaris Workflow开发系列课程介绍。
- vue.js表格赋值_vue.js input框之间赋值方法
- 关于支付回调的一些思考
- margin塌陷现象div盒子嵌套盒子外边距合并现象
- Atitit Kafka 使用总结 内容 Kafka2.0 50M1 启动 要启动zookeeper 先,比ativemp麻烦很多啊1 Kafka生产者 1 Kafka消费者2 2
- 利用IPC$空连接进行入侵及防范的方法
- 计算机核心期刊新排名(八大学报)
- 阿波罗java_携程Apollo(阿波罗)安装部署以及java整合实现
- Web答辩问题整合一
- Java消息队列三道面试题详解
- Fedora 26 安装搜狗拼音输入法 sogoupinyin
- 振弦传感器及核心VM系列振弦采集模块
热门文章
- 宝塔linux设置777权限,宝塔linux面板防护CC设置
- DNS服务器上 区域文件 是,DNS服务器上“区域文件”是用来
- 手机黑科技 中国再一次走在世界的前面
- [leetcode] 748. Shortest Completing Word
- P2E游戏西游降魔开放IDO 同步开启价值英雄NFT卡牌空投
- 使用LinkWare升级福禄克DSX2-5000网络测线仪软件版本
- C语言循环练习题(共23题)
- 【Docker】win11安装docker不能启动,wsl 2 installation is incomplete报错
- Gaia蓝牙音箱android开发,win10系统配置蓝牙模块GAiA功能的图文技巧
- npm报错:A complete log of this run can be fund in: C\Users\用户\AppData\Roaming\npm-cache_logs\解决方案(清理缓)