c++ - 继承,调用基类构造器

标签 c++ list inheritance initialization constructor

class Base{
    public:
      Base(int val):_id(val){};
      int _id;
};

class Derived : Base {
    public:
      Derived(int val):Base(_id+val){};
};

int main(){
     Derived d(60);
}

为什么不报错?基类仍未构建,但我可以使用“_id”?

谢谢

最佳答案

一个更简单的示例:int x = x + 1; 向您展示了 c++ 编译器不跟踪变量初始化。 在您的示例中,_id 存在于内存中(它有一个地址),但从未被初始化。但是,由于编译器不跟踪,所以没有错误。

关于c++ - 继承,调用基类构造器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3308565/

相关文章:

c# - .Net 中的接口(interface)继承/层次结构 - 有更好的方法吗?

c++ - unsigned int有效位数

c++ - .mp3 .wav 等音频文件的每一帧的音频

c++ - 如果 PUB 首先启动并连接到 XPUB/XSUB 代理,消息将被丢弃,直到 SUB 未连接

c++ - 事件驱动的火车模拟,似乎无法绕过它

list - 使用 xSTL 将纯文本转换为 html 样式列表,或使用 xslt 根据元素的内容和位置对元素进行分组

c++ - 继承 - C++

Python pandas数据帧从_records读取, "AssertionError: 1 columns passed, passed data had 22 columns"

Java 时间过期列表/集?

datetime - Doctrine 类表继承属性在查询时具有错误值