Containers of Generic Function Pointers

Discussion in 'C++' started by inquisitive, Feb 27, 2008.

  1. inquisitive

    inquisitive Guest

    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)(elemType&);

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

    so that I can do something like:

    int main()
    {
    std::string property;

    std::vector <Method> methodList;

    Method method;

    method.function = someMethod;

    methodList.push_back(method);

    method.function = someOtherMethod;

    methodList.push_back(method);

    std::vector<Method>::iterator it;

    for(it = methodList.begin(); 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;
    }
    inquisitive, Feb 27, 2008
    #1
    1. Advertising

  2. inquisitive

    Triple-DES Guest

    On 27 Feb, 13:45, inquisitive <> wrote:
    > Is there a way to create a collection of Method objects, for example a
    > vector or a list?


    Not the way you want. See comments below. See also my previous reply.

    >
    > template <typename elemType>
    > struct Method
    > {
    >     bool (*function)(elemType&);
    >
    >     template <typename elemType>
    >     inline bool operator()(elemType & property)
    >     {
    >         return function(property);
    >     }
    >
    > };
    >
    > so that I can do something like:
    >
    > int main()
    > {
    >     std::string property;
    >
    >     std::vector <Method> methodList;


    Which Method? Methods with different elemTypes are different types.
    You can not store different types directly in a vector.

    >
    >     Method method;
    >
    >     method.function = someMethod;


    For this to work, method has to be of type Method<int>

    >     methodList.push_back(method);
    >
    >     method.function = someOtherMethod;


    Method is now assumed to be of type Method<std::string>, which it
    isn't

    >     methodList.push_back(method);
    >
    >     std::vector<Method>::iterator it;
    >
    >     for(it = methodList.begin(); it != methodList.end(); it++)
    >     {
    >        std::cout << "Value: " << ((*it)(property)) << std::endl;


    Now you assume that all Methods are of type Method<string>. Otherwise
    this would fail.

    >     }
    >
    >    return 0;
    >
    > }
    >
    > bool someMethod(int & value)
    > {
    >     value = 25;
    >     return true;
    > }
    >
    > bool someOtherMethod(std::string & value)
    > {
    >     value = "Hello, World!";
    >     return true;
    > }


    Sorry I couldn't be of more help.

    DP
    Triple-DES, Feb 27, 2008
    #2
    1. Advertising

  3. Thomas J. Gritzan, Feb 27, 2008
    #3
  4. inquisitive

    inquisitive Guest

    Thanks to both of you.

    I suspected that I was assuming something which is fundamentally not
    possible, but got lost in the templates. Thanks for conforming.
    inquisitive, Feb 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    534
    Pete Becker
    Jan 25, 2008
  2. inquisitive
    Replies:
    2
    Views:
    390
    inquisitive
    Feb 27, 2008
  3. Ben Bacarisse

    Re: Why no generic function pointers?

    Ben Bacarisse, Jun 21, 2008, in forum: C Programming
    Replies:
    3
    Views:
    289
    Ben Bacarisse
    Jun 22, 2008
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
  5. Sebastian Mach
    Replies:
    5
    Views:
    301
Loading...

Share This Page