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 9152039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:56:48+00:00 2026-06-17T11:56:48+00:00

I am trying to use std::bind() to create a function that will call the

  • 0

I am trying to use std::bind() to create a function that will call the base class version of a virtual function rather than calling the derived class’s version.

struct Base
{
    virtual void foo() { cout << "Base\n"; }
};

struct Derived : public Base
{
    virtual void foo() { cout << "Derived\n"; }
};

int main(int argc, const char * argv[])
{
    Base* base = new Derived;
    auto baseMethodHopefully = std::bind( &Base::foo, base );
    baseMethodHopefully();    // Want call to Base::foo(), but get call to Derived::foo().

    return 0;
}

I understand from elsewhere that you can’t normally call a base function in an “anti-virtual” way such as this. The obvious exception is the common paradigm:

void Derived::bar() { Base::bar(); }

Since the expression Base::bar() is recognized as “anti-virtual” (in the sense I’m alluding to) within Derived’s methods, is it possible to bind to Base::bar() in the desired way from within one of Derived’s methods? E.g. something like:

void Derived::bar()
{
    auto baseMethod = std::bind( &Base::foo, this );
    baseMethod();
}

If so, what is the syntax?

  • 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-17T11:56:49+00:00Added an answer on June 17, 2026 at 11:56 am

    Well, &Base::foo is a member function pointer. And there is no way to use a member function pointer that doesn’t invoke a virtual override. The only syntax that avoids virtual overrides is the one where the class name, function name, and argument list are all in the same expression.

    But if you have std::bind, you likely also have lambdas, so maybe you could use:

    auto baseMethod = [this](){ return Base::foo(); };
    //...
    baseMethod();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ class where I'm trying to use std::bind1st to bind a
Im trying to use std::bind to bind this to method that is used in
I'm trying to use boost::bind with the std::sort function. I want to bind sort
I'm trying to use std::function in conjunction with std::bind , but I'm having some
I'm trying to use std::mem_fun_ref (Yes, the deprecated version. Reasons below) to call a
I'm trying to create a boost::function that allows setting of a member variable of
I am trying to use boost::bind with a boost::function using this. It seems a
I am trying to create alternative names for the function call numberedFunction when it
I'm trying to create a std::unordered_map, using a user-defined hash function and equality predicate,
I am trying to figure out the proper use of std::bind with a boost::signal2

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.