下面是代码样本惹恼我:如何从派生类访问基类中的受保护方法?
class Base {
protected:
virtual void foo() = 0;
};
class Derived : public Base {
private:
Base *b; /* Initialized by constructor, not shown here
Intended to store a pointer on an instance of any derived class of Base */
protected:
virtual void foo() { /* Some implementation */ };
virtual void foo2() {
this->b->foo(); /* Compilator sets an error: 'virtual void Base::foo() is protected' */
}
};
如何访问到受保护的overrided功能?
感谢您的帮助。 :o)
我不认为你的实施是完全正确的。为什么你有一个Base的实例作为成员变量? this-> b-> foo()会尝试调用一个纯粹的虚拟方法。 – GWW 2011-01-12 18:26:34
这个程序不应该编译。你不能实例化一个抽象类....除非`b`指向从`Base`派生的其他类的实例。 – 341008 2011-01-12 18:34:58
我省略了精确性:Derived :: b属性用于存储基类 – 2011-01-13 10:44:15