【发布时间】:2024-04-24 08:55:01
【问题描述】:
我正在学习 C++。文档docs.microsoft.com/en-us/cpp/cpp/member-access-control-cpp 说:
当您将基类指定为私有时,它只影响非静态成员。公共静态成员仍然可以在派生类中访问。
但是,下面的代码对前面引用的示例稍作调整,导致错误 C2247:
'Base::y' 不可访问,因为'Derived1' 使用'private' 从'Base' 继承。
对于这种情况,我将不胜感激。
class Base
{
public:
int x;
static int y;
};
class Derived1 : private Base
{
};
class Derived2 : public Derived1
{
public:
int ShowCount();
};
int Derived2::ShowCount()
{
int cCount = Base::y;
return cCount;
}
【问题讨论】:
-
使用
::Base::y获得正确的范围。 -
我认为微软的例子被打破了。
标签: c++ oop static-members derived-class