Containers of Generic Function Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inquisitive

    Containers of Generic Function Pointers

    Is there a way to create a collection of Method objects, for example a
    vector or a list?

    template <typename elemType>
    struct Method
    {
    bool (*function)(ele mType&);

    template <typename elemType>
    inline bool operator()(elem Type & property)
    {
    return function(proper ty);
    }
    };

    so that I can do something like:

    int main()
    {
    std::string property;

    std::vector <MethodmethodLi st;

    Method method;

    method.function = someMethod;

    methodList.push _back(method);

    method.function = someOtherMethod ;

    methodList.push _back(method);

    std::vector<Met hod>::iterator it;

    for(it = methodList.begi n(); it != methodList.end( ); it++)
    {
    std::cout << "Value: " << ((*it)(property )) << std::endl;
    }

    return 0;
    }

    bool someMethod(int & value)
    {
    value = 25;

    return true;
    }

    bool someOtherMethod (std::string & value)
    {
    value = "Hello, World!";

    return true;
    }
  • Thomas J. Gritzan

    #2
    Re: Containers of Generic Function Pointers

    inquisitive schrieb:
    Is there a way to create a collection of Method objects, for example a
    vector or a list?
    You can try boost.function which will be in the next C++ standard:


    However, the functions stored need to have the same or a compatible signature.

    --
    Thomas

    Sentences long extremely and notation Polish reverse in writing about

    Comment

    Working...