当前位置:网站首页>继承的构造函数
继承的构造函数
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){
}
};
边栏推荐
- Know MySQL database
- Sword finger offer 38 Arrangement of strings
- 在线怎么生成富文本
- Keyword static
- 2022 edition illustrated network pdf
- RDD conversion operator of spark
- Selenium element positioning (2)
- [community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
- Redis如何实现多可用区?
- Use the list component to realize the drop-down list and address list
猜你喜欢
数据工程系列精讲(第四讲): Data-centric AI 之样本工程
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
0211 embedded C language learning
Redis string type
Computer graduation design PHP part-time recruitment management system for College Students
一位博士在华为的22年
MySQL index
Unity learning notes -- 2D one-way platform production method
SPI communication protocol
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
随机推荐
[solution] every time idea starts, it will build project
Global and Chinese markets of general purpose centrifuges 2022-2028: Research Report on technology, participants, trends, market size and share
729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
阿里测开面试题
[coppeliasim] efficient conveyor belt
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
Genius storage uses documents, a browser caching tool
Redis如何实现多可用区?
Leetcode3. Implement strstr()
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
SPI communication protocol
Pangolin Library: subgraph
How to generate rich text online
剑指 Offer 30. 包含min函数的栈
2022 edition illustrated network pdf
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Sword finger offer 38 Arrangement of strings
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition