pointer to const member function typedef

前端 未结 3 1677
长情又很酷
长情又很酷 2021-01-01 11:36

I know it\'s possible to separate to create a pointer to member function like this

struct K { void func() {} };
typedef void FuncType();
typedef FuncType K::         


        
3条回答
  •  旧巷少年郎
    2021-01-01 12:23

    Another more direct way to do it (avoiding using and typedefs) is this:

    #include 
    
    class Object
    {
        int i_;
    public:
        int j_;
        Object()
            : Object(0,0)
        {}
        Object(int i, int j)
            : i_(i),
            j_(j)
        {}
    
        void printIplusJplusArgConst(int arg) const
        {
            std::cout << i_ + j_ + arg << '\n';
        }
    };
    
    int main(void)
    {
        void (Object::*mpc)(int) const = &Object::printIplusJplusArgConst;
    
        Object o{1,2};
        (o.*mpc)(3);    // prints 6
    
        return 0;
    }
    

    mpc is a const method pointer to Object.

提交回复
热议问题