【发布时间】:2020-02-28 10:06:52
【问题描述】:
这里派生类d的对象不能调用类基的受保护成员函数。
#include <iostream>
using namespace std;
class base
{
protected:
int i,j;
void setij(int a,int b)
{
i=a;
j=b;
}
void showij()
{
cout<<i<<" "<<j<<endl;
}
};
class derived : protected base
{
int k;
public:
void show()
{
base b;
b.setij(10,20);
b.showij();
}
};
int main()
{
base b;
derived d;
d.setij(3,4);
d.showij();
d.show();
return 0;
}
我希望输出是10 20,但编译器显示错误。
【问题讨论】:
-
错误是什么?
-
你不能在
derived之外调用函数,在main内。
标签: c++ inheritance access-control protected