gpt4 book ai didi

c++ - 为什么不能将继承的 protected 构造函数公开?

转载 作者:行者123 更新时间:2023-12-01 09:10:07 25 4
gpt4 key购买 nike

考虑:

class A
{
protected:
A(int) {}
void f(int) {}

public:
A() {}
};

class B : public A
{
public:
using A::A;
using A::f;
};

int main()
{
B().f(1); // ok
B(1); // error: 'A::A(int)' is protected within this context
}

为什么不能将继承的 protected构造函数设为 public,而不能将继承的 protected成员函数设为?

最佳答案

与其他成员不同,引入inherited constructor的using声明的可访问性将被忽略。

[namespace.udecl]/19

(强调我的)

A synonym created by a using-declaration has the usual accessibility for a member-declaration. A using-declarator that names a constructor does not create a synonym; instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding base class, and the accessibility of the using-declaration is ignored.

关于c++ - 为什么不能将继承的 protected 构造函数公开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62099038/

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