C++ Call Pointer To Member Function

后端 未结 4 1229

I have a list of pointers to member functions but I am having a difficult time trying to call those functions... whats the proper syntax?

typedef void (Box::         


        
4条回答
  •  伪装坚强ぢ
    2020-11-27 19:41

    Your attempt to get a member function pointer through an object betrays a misunderstanding. Member function pointers do not include a pointer to the object you call them on. You have to provide such a pointer at the point of the call.

    As many have pointed out, the syntax for a member function call is either:

     obj.*funcptr(args);
    

    or

     objptr->*funcptr(args);
    

    In the example you've given, it sounds like what you really need is a virtual function. You have a standard operation (detecting wether or not an object intersects with a box) that needs to be called on many different types of objects, the type of which can't be known at compile time. This is a job that is cut out for virtual functions.

提交回复
热议问题