c – std :: bind() – 从派生类的成员函数中创建基本保护的成员函数

前端之家收集整理的这篇文章主要介绍了c – std :: bind() – 从派生类的成员函数中创建基本保护的成员函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从派生类bind()到我的基类的函数的版本.该功能在基地被标记为保护.当我这样做时,代码在Clang(Apple LLVM Compiler 4.1)中高兴地编译,但在g 4.7.2和Visual Studio 2010中都会出现错误.错误是:“Base :: foo”:不能访问受保护的成员“.

这意味着引用的上下文实际上在bind()中,当然这个函数被看作是受保护的.但是,不应该bind()继承调用函数的上下文 – 在这种情况下,Derived :: foo() – 因此可以看到base方法是可访问的?

以下程序说明了这个问题.

struct Base
{
protected: virtual void foo() {}
};

struct Derived : public Base
{
protected:
    virtual void foo() override
    {
        Base::foo();                        // Legal

        auto fn = std::bind( &Derived::foo,std::placeholders::_1 );        // Legal but unwanted.
        fn( this );

        auto fn2 = std::bind( &Base::foo,std::placeholders::_1 );        // ILLEGAL in G++ 4.7.2 and VS2010.
        fn2( this );
    }
};

为什么行为差异?哪个是对的?为错误编译器提供了哪些解决方法

解决方法

答:请参阅 boost::bind with protected members & context引用本标准的这一部分

An additional access check beyond those described earlier in clause 11
is applied when a non-static data member or nonstatic member function
is a protected member of its naming class (11.2)105) As described
earlier,access to a protected member is granted because the reference
occurs in a friend or member of some class C. If the access is to form
a pointer to member (5.3.1),the nested-name-specifier shall name C or
a class derived from C. All other accesses involve a (possibly
implicit) object expression (5.2.5). In this case,the class of the
object expression shall be C or a class derived from C.

解决方法:使foo成为公共成员函数

#include <functional>

struct Base
{
public: virtual void foo() {}
};
原文链接:https://www.f2er.com/c/116418.html

猜你在找的C&C++相关文章