【发布时间】:2015-04-21 18:39:15
【问题描述】:
#include <iostream>
class A {
protected:
void foo()
{}
};
class B : public A {
public:
void bar()
{
std::cout << (&A::foo) << std::endl;
}
};
int main()
{
B b;
b.bar();
}
这里我试图获取基类的受保护成员函数的地址。我收到此错误。
main.cpp: In member function ‘void B::bar()’:
main.cpp:5: error: ‘void A::foo()’ is protected
main.cpp:13: error: within this context
make: *** [all] Error 1
将 foo 更改为公共工程。也打印&B::foo 作品。你能解释一下为什么我们不能得到基类的受保护成员函数的地址吗?
【问题讨论】:
-
好问题。似乎是未来的更正!
标签: c++ inheritance protected member-function-pointers