Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:49:25+00:00 2026-06-17T17:49:25+00:00

I want to bind() to my base class’s version of a function from the

  • 0

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 happily in Clang (Apple LLVM Compiler 4.1) but gives an error in both g++ 4.7.2 and in Visual Studio 2010. The error is along the lines of: “‘Base::foo’ : cannot access protected member.”

The implication is that the context for the reference is actually within bind(), where of course the function is seen as protected. But shouldn’t bind() inherit the context of the calling function–in this case, Derived::foo()–and therefore see the base method as accessible?

The following program illustrates the issue.

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 );
    }
};

Why the discrepancy in behavior? Which is correct? What workaround is available for the error-giving compilers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:49:26+00:00Added an answer on June 17, 2026 at 5:49 pm

    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 <functional>
    
    struct Base
    {
    public: virtual void foo() {}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call the base class implementation of a virtual function using a
I want to bind a function to an event but I want to change
I want to bind an event to a certain class and ID for when
I want to bind a click event with the element: $('a').click(function(){ btn_submit_pressed=false; }); Now,
I am using Luabind to expose a base class from C++ to Lua from
i want to bind gridview with my custom entity whitch i populate from data
I have a set a variable in my Base Adapter class, now I want
I want to bind a treeview to a class like this one: public class
In Boost::Spirit, how can I trigger an expectation_failure from a function bound with Boost::Bind
I want to bind a string value to a text box but only if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.