【发布时间】:2011-02-15 14:06:25
【问题描述】:
假设我有以下类层次结构:
class Base
{
protected:
virtual void foo() = 0;
friend class Other;
};
class Derived : public Base
{
protected:
void foo() { /* Some implementation */ };
};
class Other
{
public:
void bar()
{
Derived* a = new Derived();
a->foo(); // Compiler error: foo() is protected within this context
};
};
我想我也可以更改它a->Base::foo(),但由于foo() 在Base 类中是纯虚拟的,因此无论如何调用都会导致调用Derived::foo()。
但是,编译器似乎拒绝a->foo()。我想这是合乎逻辑的,但我真的不明白为什么。我错过了什么吗?它不能(不应该)处理这种特殊情况吗?
谢谢。
【问题讨论】:
标签: c++ inheritance compiler-errors friend