Why do I get C2248 (inaccessible member) with a protected static member?

女生的网名这么多〃 提交于 2019-12-31 03:55:09

问题


Let's say I have:

#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;
    }
}

When I try to compile this, I get "m_intList: cannot access inaccessible member declared in class 'MoreDerived'".

Question: Why can't I access a protected static member in the derived class's DoStuff function?


回答1:


class Derived : Base means class Derived : private Base. The behaviour of private inheritance is:

  • protected members of the base class become private members of the derived class.
  • private members of the base class have no access as members of the derived class.

So m_intList is:

  • protected in Base
  • private in Derived
  • no access in MoreDerived

hence your error.



来源:https://stackoverflow.com/questions/42966024/why-do-i-get-c2248-inaccessible-member-with-a-protected-static-member

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!