我是 C++ 的新手。
class A
{
public:
int i;
protected: //**--- [1]**
void set()
{
i=5;
cout<<i;
}
};
class B : public A
{
public:
void call()
{
A obj;
obj.set(); //**----[2]**
set(); //**---[3]**
}
};
int main()
{
B* b_obj = new B;
b_obj->call();
}
如果我尝试包含[2]并且不将[1]替换为公共但如果我单独编译包含[3]它可以工作,为什么不编译代码?
编译错误:错误:'void A::set()' 受保护。
简而言之,我的目的是了解如果基类接口的访问说明符设置为受保护,为什么不能在派生类中调用基对象。