gpt4 book ai didi

inheritance - 派生类的大括号(无构造函数)初始化

转载 作者:行者123 更新时间:2023-12-04 04:41:59 27 4
gpt4 key购买 nike

struct base {
};

//struct derived { // <--- this one works
struct derived : public base { // <--- but this one doesn't
double x;
};

main () {
derived d{0.5};
return 0;
}

使用大括号初始化derived 的方法是什么,意思是不用显式编写构造函数?

最佳答案

只有 aggregates可以通过这种方式初始化(​​无需定义您自己的构造函数)并且成为聚合的要求之一是没有任何基类。简而言之,您不能那样做。

关于inheritance - 派生类的大括号(无构造函数)初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com