c++ - 为什么我会得到带有 protected 静态成员的 C2248(无法访问的成员)?

标签 c++ inheritance

假设我有:

#include <Windows.h>
#include <iostream>
#include <vector>

std::vector<int> Base::m_intList;

class Base
{
public:
    Base();
protected:
    static std::vector<int> m_intList;
};

class Derived : Base
{
public:
    Derived();
protected:
    bool fWhatever;
};

class MoreDerived : Derived
{
public:
    MoreDerived();
private:
    HRESULT DoStuff();
};

Base::Base()
{

}

Derived::Derived()
{

}

MoreDerived::MoreDerived()
{

}

HRESULT MoreDerived::DoStuff()
{
    for (auto it = m_intList.begin(); it != m_intList.end(); it++)
    {
        std::cout << *it;
    }
}

当我尝试编译它时,我得到“m_intList:无法访问类‘MoreDerived’中声明的不可访问成员”。

问题:为什么我不能在派生类的 DoStuff 函数中访问 protected 静态成员?

最佳答案

class Derived : Base 表示class Derived : private Base。私有(private)继承的行为是:

    基类的
  • protected 成员成为派生类的 private 成员。
  • private 基类的成员作为派生类的成员没有访问权限

所以 m_intList 是:

    Base
  • protected
  • privateDerived
  • 无法访问 MoreDerived

因此你的错误。

关于c++ - 为什么我会得到带有 protected 静态成员的 C2248(无法访问的成员)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42966024/

相关文章:

java - 我如何知道这属于哪个继承类?

c++ - 如何在 C++ 客户端实现在 DLL 中声明的方法

c++ - 智能指针无法释放内存

c++ - 仅在 travis-ci 上测试失败;不支持 RTTI?

c# - 只允许为特定类实现接口(interface)

java - 您能否分析一下有关继承的 JAVA 代码的输出

c++ - 新的,Microsoft 除外

c++ - 使用动态库是否节省内存

c++ - 共享库 : no version information available

c++ - 模板参数约束