gpt4 book ai didi

c++ - 继承构造函数并提供新的重载 : no arguments base constructor seems to not participate in overload resolution

转载 作者:太空狗 更新时间:2023-10-29 21:18:58 25 4
gpt4 key购买 nike

<分区>

测试显示奇怪的行为(c++14g++ 4.9.1clang 3.5.5):

总结一下:

  • 如果 B 没有提供它可以使用的其他构造函数 A::A()
  • 如果 B 提供它不能使用 A::A() 的其他构造函数,但它使用 A::A(无论参数) ,这是意外行为(至少对我而言)。

设置 1:

struct A {
A() {};
A(int) {}; // with or without this overload the result are the same
};

struct B : A {
using A::A;
};

B b0{}; // OK

设置 2:

struct A {
A() {}; // with a default constructor instead (empty class A)
// the results are the same
};

struct B : A {
using A::A;
B(int){}
};

B b0{}; // no matching constructor
B b1{24}; // OK

设置 3:

struct A {
A() {};
A(int) {};
};

struct B : A {
using A::A;
B(int, int){}
};

B b0{}; // no matching constructor
B b1{24}; // OK
B b2{24, 42}; // OK

为什么会发生这种情况,如何“修复”。

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