vector of generic function pointers

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

  1. inquisitive

    inquisitive Guest

    Is it possible to have a vector of generic function pointers?

    I am able to devise a generic function pointer, this way:

    template <typename elemType, elemType (*function)(std::string&)>
    struct Method
    {
    inline elemType operator()(std::string & property)
    {
    return function(property);
    }
    };

    It is instantiated as:
    Method <int, methodOne> numberMethod;

    Now I want to create a vector of these generic function pointers, how
    do I declare such a vector and how do I initialize its elements:

    I found this Scott Meyer's link, which maybe helpful - it is giving me
    hint but I am not able to gain my solution from it: http://www.ddj.com/cpp/184401580
    inquisitive, Feb 27, 2008
    #1
    1. Advertising

  2. inquisitive

    Triple-DES Guest

    On 27 Feb, 08:04, inquisitive <> wrote:
    > Is it possible to have a vector of generic function pointers?
    >
    > I am able to devise a generic function pointer, this way:
    >
    > template <typename elemType, elemType (*function)(std::string&)>
    > struct Method
    > {
    >     inline elemType operator()(std::string & property)
    >     {
    >         return function(property);
    >     }
    >
    > };
    >
    > It is instantiated as:
    >  Method <int, methodOne> numberMethod;
    >
    > Now I want to create a vector of these generic function pointers, how
    > do I declare such a vector and how do I initialize its elements:


    You could make a common base class:

    template<typename E>
    struct MethodBase
    {
    virtual E operator()(const std::string&) const = 0;
    };
    template<typename E, E(*f)(const std::string&)>
    struct Method : public MethodBase<E>
    {
    // ...
    };

    int main()
    {
    std::vector<MethodBase<int> *> v;
    v.push_back( new M<int, someFunction>);
    v.push_back( new M<int, someFunction2>);
    // cleanup
    }
    Triple-DES, Feb 27, 2008
    #2
    1. Advertising

  3. inquisitive

    inquisitive Guest

    On Feb 27, 2:07 pm, Triple-DES <> wrote:
    > On 27 Feb, 08:04, inquisitive <> wrote:
    >
    >
    >
    > > Is it possible to have a vector of generic function pointers?

    >
    > > I am able to devise a generic function pointer, this way:

    >
    > > template <typename elemType, elemType (*function)(std::string&)>
    > > struct Method
    > > {
    > > inline elemType operator()(std::string & property)
    > > {
    > > return function(property);
    > > }

    >
    > > };

    >
    > > It is instantiated as:
    > > Method <int, methodOne> numberMethod;

    >
    > > Now I want to create a vector of these generic function pointers, how
    > > do I declare such a vector and how do I initialize its elements:

    >
    > You could make a common base class:
    >
    > template<typename E>
    > struct MethodBase
    > {
    > virtual E operator()(const std::string&) const = 0;};
    >
    > template<typename E, E(*f)(const std::string&)>
    > struct Method : public MethodBase<E>
    > {
    > // ...
    >
    > };
    >
    > int main()
    > {
    > std::vector<MethodBase<int> *> v;
    > v.push_back( new M<int, someFunction>);
    > v.push_back( new M<int, someFunction2>);
    > // cleanup
    >
    > }


    Thank you, but I want the independence to have different return-types
    for my functions.

    The problem that I was facing is to as to how could I templatize the
    parameter passed on to the vector at its declaration.
    My core issue is to create a generic function pointers with the
    signature known in advance but not the actual type of the parameter
    and return value. And then how do I build a collection of such generic
    function pointers.

    A second question is, if the vector and the functions are part of a
    class, how would the declaration look like.
    inquisitive, Feb 27, 2008
    #3
    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. pmatos
    Replies:
    6
    Views:
    23,727
  2. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    543
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,553
    Francesco S. Carta
    Sep 7, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    653
Loading...

Share This Page