Calling C++ class methods via a function pointer

前端 未结 10 928
醉酒成梦
醉酒成梦 2020-11-22 06:47

How do I obtain a function pointer for a class member function, and later call that member function with a specific object? I’d like to write:

class Dog : A         


        
10条回答
  •  执笔经年
    2020-11-22 07:05

    Read this for detail :

    // 1 define a function pointer and initialize to NULL
    
    int (TMyClass::*pt2ConstMember)(float, char, char) const = NULL;
    
    // C++
    
    class TMyClass
    {
    public:
       int DoIt(float a, char b, char c){ cout << "TMyClass::DoIt"<< endl; return a+b+c;};
       int DoMore(float a, char b, char c) const
             { cout << "TMyClass::DoMore" << endl; return a-b+c; };
    
       /* more of TMyClass */
    };
    pt2ConstMember = &TMyClass::DoIt; // note:  may also legally point to &DoMore
    
    // Calling Function using Function Pointer
    
    (*this.*pt2ConstMember)(12, 'a', 'b');
    

提交回复
热议问题