首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >公共访问声明不会影响成员函数指针?

公共访问声明不会影响成员函数指针?
EN

Stack Overflow用户
提问于 2015-07-20 21:47:58
回答 3查看 495关注 0票数 18

在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。

注意:这是一个关于语言的问题,不是类设计的问题。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31518214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档