假设我有:
#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'中声明的不可访问的成员”。
Question: 为什么我无法访问派生类的 DoStuff 函数中的受保护静态成员?
class Derived : Base
means class Derived : private Base
。私有继承的行为是:
-
protected
基类的成员成为private
派生类的成员。
-
private
基类的成员有无法访问作为派生类的成员。
So m_intList
is:
-
protected
in Base
-
private
in Derived
-
无法访问 in
MoreDerived
因此你的错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)