Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use std::bind() to call the base class's version of a virtual function?

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?

like image 610
OldPeculier Avatar asked Jan 18 '13 21:01

OldPeculier


People also ask

How do you call a base class virtual function in C++?

When you want to call a specific base class's version of a virtual function, just qualify it with the name of the class you are after, as I did in Example 8-16: p->Base::foo(); This will call the version of foo defined for Base , and not the one defined for whatever subclass of Base p points to.

What is the use of std :: bind?

std::bind. The function template bind generates a forwarding call wrapper for f . Calling this wrapper is equivalent to invoking f with some of its arguments bound to args .

How do you call a derived function from base class?

A virtual function is a member function of a base class that is overridden by a derived class. When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class's version of the function.

How do you call base class overridden in C++?

Access Overridden Function in C++ To access the overridden function of the base class, we use the scope resolution operator :: . We can also access the overridden function by using a pointer of the base class to point to an object of the derived class and then calling the function from that pointer.


1 Answers

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();
like image 198
aschepler Avatar answered Sep 21 '22 05:09

aschepler