Craig Craig -4 years ago 99
C++ Question

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

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download