当前位置:网站首页>继承的构造函数
继承的构造函数
2022-07-06 02:17:00 【_Jxyz】
继承的构造函数
继承的构造函数不能初始化派生类的成员,可以使用类内初始化,或则追加构造函数
- using声明语句只是令某个名字在当前作用域内可见。而当作用于构造函数的时候,会产生代码。
using Base::Base;
- 对于基类的每个构造函数,编译器都生成一个与之对应的派生类构造函数
- 不会改变构造函数的控制权限,比如说基类中的构造函数时private,继承在派生类中的也是private
- 由于using只能指定名字,所以基类中构造函数如果存在explicit 和 constexpr关键及同样会被继承。
- 如果一个基类的构造函数含有默认实参,将会生成多个构造函数,其中每个构造函数分别省略掉一个含有默认实参的形参。比如基类有一个两个参数的构造函数,第二个参数为默认实参,则会参数一个两个参数的构造函数(都不是默认实参)和一个参数(第一个参数)的构造函数
- 默认、拷贝、移动构造函数不会被继承, 这些构造函数按照正常的规则被合成
C++11 标准规定,继承构造函数与类的一些默认函数(默认构造、析构、拷贝构造函数等)一样,是隐式声明,如果一个继承构造函数不被相关代码使用,编译器不会为其产生真正的函数代码。这样比通过派生类构造函数“透传构造函数参数”来完成基类初始化的方式,总是需要定义派生类的各种构造函数更加节省目标代码空间。
多继承的情况下,
继承构造函数会出现“冲突”的情况,因为多个基类中的部分构造函数可能导致派生类中的继承构造函数的函数名与参数相同
class A {
public:
A(int i){
}
};
class B {
public:
B(int i){
}
};
class C : public A, public B {
public:
// using A::A; //编译出错,重复定义C(int)
// using B::B; //编译出错,重复定义C(int)
// 显示定义继承构造函数 C(int)
C(int i):A(i),B(i){
}
};
边栏推荐
- How does redis implement multiple zones?
- Online reservation system of sports venues based on PHP
- [coppeliasim] 6-DOF path planning
- 【机器人库】 awesome-robotics-libraries
- Grabbing and sorting out external articles -- status bar [4]
- Use Scrollview and tabhost to realize vertical scrollbars and tabs
- Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
- Campus second-hand transaction based on wechat applet
- Computer graduation design PHP part-time recruitment management system for College Students
- 剑指 Offer 30. 包含min函数的栈
猜你喜欢
数据工程系列精讲(第四讲): Data-centric AI 之样本工程
Overview of spark RDD
How to generate rich text online
[solution] add multiple directories in different parts of the same word document
Computer graduation design PHP college classroom application management system
500 lines of code to understand the principle of mecached cache client driver
Paper notes: graph neural network gat
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
Redis string type
随机推荐
Use the list component to realize the drop-down list and address list
安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3
Number conclusion LC skimming review - 1
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
一位博士在华为的22年
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
Global and Chinese markets of nasal oxygen tubes 2022-2028: Research Report on technology, participants, trends, market size and share
怎么检查GBase 8c数据库中的锁信息?
从顶会论文看2022年推荐系统序列建模的趋势
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
[robot hand eye calibration] eye in hand
SSM assembly
HDU_p1237_简单计算器_stack
Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
2022 edition illustrated network pdf
零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
【coppeliasim】6自由度路径规划
Overview of spark RDD
Apicloud openframe realizes the transfer and return of parameters to the previous page - basic improvement
[solution] every time idea starts, it will build project