在g++ (5.1版)下,我有一个关于访问声明的问题。
class Base
{
public:
void doStuff() {}
};
class Derived : private Base
{
public:
// Using older access declaration (without using) shoots a warning
// and results in the same compilation error
using Base::doStuff;
};
template<class C, typename Func>
void exec(C *c, Func func)
{
(c->*func)();
}
int main()
{
Derived d;
// Until here, everything compiles fine
d.doStuff();
// For some reason, I can't access the function pointer
exec(&d,&Derived::doStuff);
}
g++无法编译上述代码,出现以下错误:
test.cpp: In实例化‘void exec(C*,Func) with C= Derived;Func =void(Base::*)()’:test.cpp:24:27:此处需要
函数:错误:‘base’是‘test.cpp:17:4’(c->*func)()的一个不可访问的基;
即使函数本身可以被调用(d.doStuff();
),也不能使用指针,即使我声明函数是可以从外部访问的。私有继承在某种程度上也很重要,因为Derived
类选择只公开基类中的特定成员集,基类是接口实现IRL。
注意:这是一个关于语言的问题,不是类设计的问题。
https://stackoverflow.com/questions/31518214
复制相似问题