gpt4 book ai didi

c++ - 删除构造函数继承

转载 作者:可可西里 更新时间:2023-11-01 17:50:22 24 4
gpt4 key购买 nike

构造函数继承的一个要求是派生类不能有任何具有相同签名的构造函数。但是,我不确定已删除的函数在这些规则下的行为如何。

class Foo
{
public:
Foo() = delete;
Foo(const Foo& a_Foo) = delete;
Foo(int a_Value) : m_Value(a_Value) {}

private:
int m_Value;
};

class Bar : public Foo
{
public:
using Foo::Foo;
Bar() : Foo(7) {};
Bar(const Bar& a_Bar) : Foo(12) {};
};
  • 是否继承了已删除的构造函数?
  • 如果是这样,Bar()Foo() 具有相同的签名,这是否会使代码无效?
  • 您可能会争辩说 Foo(const Foo& a_Foo)Bar(const Bar& a_Bar) 具有不同的签名。复制构造函数在构造函数继承下如何表现?

最佳答案

默认、复制和移动构造函数被继承,继承构造函数也不能隐式声明派生类的复制或移动构造函数。此外,如果派生类中已经存在具有相同签名的构造函数,则继承构造函数声明基本上只会“跳过”基类构造函数。

For each non-template constructor in the candidate set of inherited constructors other than a constructor having no parameters or a copy/move constructor having a single parameter, a constructor is implicitly declared with the same constructor characteristics unless there is a user-declared constructor with the same signature in the complete class where the using-declaration appears or the constructor would be a default, copy, or move constructor for that class.

([class.inhctor]/3)

此外,如果删除相应的基类构造函数,则继承的构造函数也会被删除。

A constructor so declared has the same access as the corresponding constructor in X. It is deleted if the corresponding constructor in X is deleted (8.4). An inheriting constructor shall not be explicitly instantiated (14.7.2) or explicitly specialized (14.7.3).

([class.inhctor]/4)

关于c++ - 删除构造函数继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33376021/

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