考虑以下例子
class base
{
protected :
int x = 5;
int(base::*g);
};
class derived :public base
{
void declare_value();
derived();
};
void derived:: declare_value()
{
g = &base::x;
}
derived::derived()
:base()
{}
据我所知,只有基类的友元和派生类能够访问基类的受保护成员,但在上述示例中,我收到以下错误消息:“Error C2248 'base::x': cannot access protected member declared in class",但当我添加以下行时:
根据我的了解,只有基类的朋友和派生类可以访问基类的受保护成员,但在上面的例子中,我得到了以下错误:"Error C2248 'base::x': cannot access protected member declared in class "
但是当我添加以下行:
friend class derived;
将它声明为友元后,我可以访问基类的成员,那我在声明派生类时有做错什么吗?