std::bind()-ing a base protected member function from a derived class's member function

前端 未结 2 1119
感情败类
感情败类 2021-01-17 17:07

I want to bind() to my base class\'s version of a function from the derived class. The function is marked protected in the base. When I do so, the code compiles

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 17:41

    Answer: see boost::bind with protected members & context which quotes this part of the Standard

    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.

    Workaround: make foo a public member function

    #include 
    
    struct Base
    {
    public: virtual void foo() {}
    };
    

提交回复
热议问题