gpt4 book ai didi

c++ - C++ 中的私有(private)基类可访问性

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:11 24 4
gpt4 key购买 nike

我最近不得不做这样的事情:

class A { };
class B : private A { };
class C : public B {
public:
A *myA;
};

int main() {
return 0;
}

而且我在尝试的三个编译器中都遇到了错误。当我将 myA 的声明更改为 ::A *myA 时,一切正常。我查阅了 C++ 标准,发现第 11.2 节第 3 段说:

Note: A member of a private base class might be inaccessible as an inherited member name, but accessible directly.

哪个相关,但不清楚。为什么名称 A 不可访问?如果不隐藏A会出现什么问题?

谢谢,
-本

最佳答案

它可能“出错”的地方:

namespace nmsp
{
class A {};
}

class A {};

class B : private nmsp::A
{
// well-formed:
A* d; // refers to the injected-class-name nmsp::A!!
};

class C : public B
{
// ill-formed
A* p; // refers to the injected-class-name nmsp::A!!
};

无论是使用::A 还是nmsp::A 都不应该依赖于base-clause 中的access-specifier,否则会报错-俯卧 IMO。

关于c++ - C++ 中的私有(private)基类可访问性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826325/

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