为什么我继承的构造函数调用我的基本默认构造函数
Why my inherited constructor call my base default constructor
也许我只是向谷歌提出了我的问题,但我找不到问题的答案。我的麻烦是我继承的构造函数调用了我的默认基构造函数,我真的不明白为什么。这是我的简化版本。
例:
答.cpp
#include <iostream>
#include "A.h"
using namespace std;
A::A()
{
cout << "A" << endl;
}
B.cpp
#include <iostream>
#include "B.h"
using namespace std;
B::B()
{
cout << "B" << endl;
}
B::B(int x)
{
cout << "B" << x << endl;
}
资料来源.cpp
#include <iostream>
#include "A.h"
#include "B.h"
using namespace std;
int main() {
B * b = new B();
cout << "---" << endl;
B * b2 = new B(2);
system("PAUSE");
return 0;
}
输出:
A
B
---
A
B2
Press any key to continue . . .
我只是想看看 B 构造函数是做什么的。喜欢这个:
B
---
B2
Press any key to continue . . .
因为父类可能负责初始化子类稍后依赖的成员变量(包括可能分配内存)。
相关文章:
- 为什么我的开关/机箱在使用枚举时默认?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 是否有 C++20 浮点数的包装器,使我能够默认宇宙飞船操作员?
- 为什么我的对象声明不调用默认构造函数?
- 为什么当我进入退出条件时,程序会打印两倍的默认条件而不是退出 while 循环?
- 谷歌嘲笑!我的测试出错.默认情况下,呼叫将返回调用
- 我可以在哪里放置参数的默认值
- 为什么QTableView扩展选择忽略了我的默认选择行
- 为什么我的默认参数被忽略
- 为什么我不能对具有成员初始值设定项列表的默认 ctor 使用 =default。
- 为什么我的自定义分配器比默认分配器慢
- 为什么我的手写复制构造函数会生成与默认的代码不同的代码
- ALSA 的snd_pcm_get_chmap总是在 Ubuntu 18.04 上在我的默认设备(脉冲)上返回 NULL。
- 错误C4430:丢失类型指定词 - 假设INT.注意:C 不支持对我的构造函数的默认设置
- 即使我定义了一个,我的默认构造函数也没有被调用
- 如何在我的子类控件paint方法中在默认的paint结果之上绘制一些东西
- C++ 为什么我不能在已删除的默认构造函数的对象上使用交换
- “在成员函数之外封闭类的定义中需要默认成员初始值设定项” - 我的代码格式不正确
- 为什么 C++11 隐式删除我的默认构造函数
- 为什么CUPS没有返回我的默认打印机