How to get the address of an overloaded member function?

前端 未结 1 1021
不知归路
不知归路 2020-12-10 02:11

I\'m trying to get a pointer to a specific version of an overloaded member function. Here\'s the example:

class C
{
  bool f(int) { ... }
           


        
相关标签:
1条回答
  • 2020-12-10 02:25

    Well, i'll answer what i put as comment already so it can be accepted. Problem is with constness:

    class C
    {
      bool f(int) { ... }
      bool f(double) const { ... }
    
      bool example()
      {
        // I want to get the "double" version.
        typedef bool (C::*MemberFunctionType)(double) const; // const required!
        MemberFunctionType pointer = &C::f;
      }
    };
    

    Clarification:

    The original question didn't contain that const. I did a wild guess in the comments whether he possibly has f being a const member function in the real code (because at a yet earlier iteration, it turned out yet another thing was missing/different to the real-world code :p). He actually had it being a const member function, and told me i should post this as an answer.

    0 讨论(0)
提交回复
热议问题