问题
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:
protectedmembers of the base class becomeprivatemembers of the derived class.privatemembers of the base class have no access as members of the derived class.
So m_intList is:
protectedinBaseprivateinDerived- no access in
MoreDerived
hence your error.
来源:https://stackoverflow.com/questions/42966024/why-do-i-get-c2248-inaccessible-member-with-a-protected-static-member