28
votes

In C++, it's possible to get a pointer to a (non-static) member function of a class, and then later invoke it on an object. If the function was virtual, the call is dispatched dynamically depending on the dynamic type of the object. It's also possible (not using a member pointer) to call virtual member functions of objects monomorphically, by explicitly providing the scope containing the version to use. The following code demonstrates this:

#include <iostream>
using std::cout; using std::endl;

struct Foo
{
    virtual void foo() { cout << 1 << endl; }
};

struct Foo2: public Foo
{
    virtual void foo() { cout << 2 << endl; }
};

int main( int, char** )
{
    Foo *foo = new Foo2;

    void (Foo::*foo_pointer)() = &Foo::foo;

    foo->foo();            // prints 2
    foo->Foo::foo();       // prints 1
    (foo->*foo_pointer)(); // prints 2
}

What I would like to do is combine the two, and get a pointer to the monomorphic version of a member function; i.e., I want a pointer to Foo::foo which always calls the base class version of foo, and prints 1, even if it is invoked on a Foo2. However, I haven't been able to find a way to do this. Is it possible?

(Other than the tedious manual way of writing a new non-virtual function which makes the monomorphic call, and then getting a pointer to that.)

3
A new SO user asking a good question with compilable code snippets? Now I don't see that everyday! :-)In silico
A pretty tough question even.Alexandre C.
I wonder why you would want to do this, and whether there isn't a better way to achieve whatever you want to achieve with that. (Oh, and I don't think this is possible, but C++ keeps surprising me, so I won't blurt out with an answer saying so.)sbi
@illissius: have you considered using lambdas or std::function to do that? GCC and VC10 support these.sbi
@illissius: How would you realize whether a virtual function has or hasn't been overriden from the other language?ali_bahoo

3 Answers

11
votes

It's possible in GCC, but the way it's documented in C++ language extensions section suggests there's no portable way to do it.

You can do two things:

  1. If you control the class, create a non-virtual function and a virtual wrapper for it and when you know you don't need virtual dispatch, just take address of the non-virtual one.
  2. If you don't, create a template functor that will hold the member pointer and do the explicit scope call.
0
votes

In other words : you want to cheat.

No, it is not possible because that is how the polymorphism in combination with pointer to member method works.

0
votes

To elaborate with a code example for a wrapper function (and despite the fact the OP wanted to avoid this method!) as in many cases this is the pragmatically preferable solution:

#include <iostream>
using std::cout; using std::endl;

struct Foo
{
    virtual void foo() { cout << 1 << endl; }
};

struct Foo2: public Foo
{
    virtual void foo() { cout << 2 << endl; }
};

void monomorphicFooFoo( Foo * f ) { f->Foo::foo(); }

int main()
{
    Foo *foo = new Foo2;

    void (*baseFoo)( Foo * ) = &monomorphicFooFoo;
    baseFoo( foo ); // Prints 1
}