boost::bind with protected members & context

前端 未结 3 574
不知归路
不知归路 2020-12-17 01:29

In the below code, there are two \"equivalent\" calls to std::for_each using boost:bind expressions. The indicated line compiles, the indicated fai

3条回答
  •  醉梦人生
    2020-12-17 01:55

    Actually, this seems logical. Inheritance gives you access to Derived::foo and not to Base::foo. Let me illustrate with a code example:

    struct Derived : public Base
    {
        void callPrivateMethod(Base &b)
        {
            // this should obviously fail
            b.foo(5);
    
            // pointer-to-member call should also fail
            void (Base::*pBaseFoo) (int) = &Base::foo; // the same error as yours here
            (b.*pBaseFoo)(5);
        }
    };
    

提交回复
热议问题