【发布时间】:2014-11-03 06:07:35
【问题描述】:
Class Base() {
protected:
void foo();
}
Class Derived : public Base {
void bar();
}
void Derived::bar(){
foo(); //this causes an error.
}
我知道我可能遗漏了一些明显的东西,但我已经转了一个小时。如何在派生类中调用受保护的函数?
【问题讨论】:
-
会导致什么错误?
-
你试过 Base::foo() 吗?
-
请注意,Derived 类中的 bar() 方法是私有的,因为这是没有访问说明符的类中方法的默认可见性。
-
听起来您忽略了将任何包含
Base::foo()定义的 .cpp 文件添加到您的项目文件(或任何与您的构建工具等效的文件) -
-1 不包括错误,并且不发布实际代码。您认为您的问题与
protected的方法有关,但您错了。
标签: c++ inheritance