gpt4 book ai didi

c++ - 非默认构造函数类型的继承构造函数 + 类内初始化失败

转载 作者:IT老高 更新时间:2023-10-28 22:30:22 27 4
gpt4 key购买 nike

我在我的项目中遇到以下错误:

error: use of deleted function ‘C::C(int)’ note: ‘C::C(int)’ isimplicitly deleted because the default definition would be ill-formed:error: use of deleted function ‘M::M()’

This is the code I am using:

struct M {
M(int){}
M() = delete; // Allowing this would work.
};

struct B {
B(int) {}
B() = delete;
};

struct C : public B {
using B::B;
M n = {5};

// C(int i) : B(i) {} // Adding this would work
};

C c{1};

有人知道为什么会这样吗?


显然,该语言愿意在继承的构造函数的末尾附加更多初始化(因为它愿意调用默认构造函数)。显然,它愿意在显式定义的构造函数的末尾隐式添加对非默认构造函数(类内初始化)的调用。但是由于某种我不明白的原因,它不愿意同时做这两个。

根据this question , 完美转发还不够完美,不应该在这里使用。

注意:在实际情况下,B 的构造函数要复杂得多,并且可能会发生变化,因此手动转发东西并不是一个真正可行的选择。

最佳答案

关于c++ - 非默认构造函数类型的继承构造函数 + 类内初始化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574406/

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