C++: Pointer to monomorphic version of virtual member function?

前端 未结 3 1938
悲&欢浪女
悲&欢浪女 2020-12-05 02:55

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 dispatc

3条回答
  •  抹茶落季
    2020-12-05 03:07

    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.

提交回复
热议问题