如何使用函数列表指向矢量对象中的成员函数?

时间:2021-11-11 21:00:08
struct object
{
    void function()
    {
        std::cout << "function" << std::endl;
    }
};

int main()
{
    // create vectors for objects and functions)
    std::vector<object*> objectvec;
    std::vector<void*> functionlist;
    objectvec.push_back(new object);

    // create a pointer to an object's function

    void (object::* ptfptr_function) (void) = &object::function;
    functionlist.push_back(&ptfptr_tryfunc);

    // how do I call "functionvec[0]->tryfunc()" using the functionlist?
    // (following line does nothing:)

    functionlist[0];
}

1 个解决方案

#1


2  

You want this:

你要这个:

std::vector<void(object::*)()> functionlist;    // container

functionlist.push_back(&object::function);      // add pointer-to-member-fn

(objectvec[0]->*functionlist[0])();             // invoke ptmf on an instance

#1


2  

You want this:

你要这个:

std::vector<void(object::*)()> functionlist;    // container

functionlist.push_back(&object::function);      // add pointer-to-member-fn

(objectvec[0]->*functionlist[0])();             // invoke ptmf on an instance