C ++ boost :: bind说不可访问的基类 [英] C++ boost::bind says inaccessible base class

查看:138
本文介绍了C ++ boost :: bind说不可访问的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 boost :: bind 来调用类中的成员函数。通常这个工作正常,但在这种特殊情况下,我的编译器(GCC)抱怨,我想使用一个不可访问的基类,当我不是。

I am trying to use boost::bind to call a member function in a class. Normally this works fine, but in this particular case my compiler (GCC) is complaining that I am trying to use an inaccessible base class when I am not.

这里是一些演示问题的代码。我做错了什么?

Here is some code that demonstrates the problem. What am I doing wrong?

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

class base
{
    protected:
        void test()
        {
            std::cout << "base::test()\n";
        }
};

class derived: virtual protected base
{
    public:
        using base::test;
};

int main(void)
{
    derived d;

    // This works, calling derived::test(), which in turn calls base::test()
    d.test();

    // This does not work, saying 'base' is an inaccessible base of 'derived',
    // but I am not binding base::test, I am binding derived::test.
    boost::function<void()> fn;
    fn = boost::bind(&derived::test, d);
    fn();

    return 0;
}


推荐答案

using 声明没有定义一个函数。它声明一个名称(不是一个函数!),并且取消隐藏基本名称。这是真的,声明本身有自己的可访问性级别,这就是为什么你一开始使用它,但再次强调:使用声明不声明一个新的成员函数。例如。 C ++ 11 7.3.3 / 11:

The using declaration doesn't define a function. It "declares a name" (not a function!), and unhides base names. It's true that the declaration itself has its own accessibility level, which is why you're using it at all in the first place, but to stress again: The using declaration does not declare a new member function. E.g. C++11 7.3.3/11:


使用声明

它的定义,在你的情况下,仍然是 void base :: test(){} 是派生类知道的实体, code>& derived :: test 。你不能得到类型 void(derived:**)()的函数指针,因为没有这样的函数。

"Its definition", in your case, is still void base::test(){}, and that is the entity that is made known to the derived class and referred to by &derived::test. You can't get a function pointer of type void(derived:**)() from this because there is no such function.

当你说& derived :: test 时,使用& (5.3.1 / 3):

When you say &derived::test, the &-operator is used, which works like this (5.3.1/3):


一元的结果& 运算符是指向其操作数的指针。操作数应为左值或 qualified-id 。如果操作数是一个 C m 的 qualified-id c>,类型 T ,结果具有类型 C > T ,并且是指定C :: m的prvalue。

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static member m of some class C with type T, the result has type "pointer to member of class C of type T" and is a prvalue designating C::m.

& derived :: test 命名类 base的非静态成员 test 。 [感谢@DyP:]在10.2 / 3中正式化:

In my interpretation of the above logic, &derived::test "names the non-static member test of class base". [Thanks to @DyP:] The is formalized in 10.2/3:


f C [...]由[...]在声明集中, 会被他们指​​定的成员替换

The lookup set for f in C [...] consists of [...] the declaration set [...]. In the declaration set, using-declarations are replaced by the members they designate

您可以提供这样的包装:

If you really want, you can provide a wrapper like this:

class derived : protected base
{
public:
    void test() { base::test(); }
};

(奇怪的是,该解决方案实际上 隐藏基本函数,而不是使用使用,我们使用显式限定名称来引用基函数。)

(Curiously, that solution actually does hide the base function, which is what we want. Rather than using using, we use an explicitly qualified name to refer to the base function.)

这篇关于C ++ boost :: bind说不可访问的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆