pointer to function as template parameter

Discussion in 'C++' started by MaxMax, May 28, 2006.

  1. MaxMax

    MaxMax Guest

    I have a

    template<class T, class TBase>
    TBase* MyFunc(int *a, int *b)
    {
    T* pT = new T;
    return static_cast<TBase*>(pT);
    }

    (the a and the b are used in some way in the true function)

    (clearly TBase must be a base class of T to make it work)

    I want to create something like

    list<pointerfunctions> a;

    How do I write the signature of the function? I can't use typedef because
    there is a template...

    --- Thanks
     
    MaxMax, May 28, 2006
    #1
    1. Advertising

  2. MaxMax

    Tomás Guest

    MaxMax posted:


    > I want to create something like
    >
    > list<pointerfunctions> a;
    >
    > How do I write the signature of the function? I can't use typedef
    > because there is a template...



    Please elaborate on the restriction -- I don't see any reason why you
    couldn't use typedef.


    list< void (*)() > a;


    /* Or alternatively */

    typedef void (*SimpleFunc)();

    list< SimpleFunc > b;



    -Tomás
     
    Tomás, May 28, 2006
    #2
    1. Advertising

  3. MaxMax

    Cy Edmunds Guest

    "MaxMax" <> wrote in message
    news:%Uieg.15708$...
    >I have a
    >
    > template<class T, class TBase>
    > TBase* MyFunc(int *a, int *b)
    > {
    > T* pT = new T;
    > return static_cast<TBase*>(pT);


    Why the cast? pT is already a TBase*. And isn't this design likely to result
    in memory leaks? This is mysterious stuff.

    > }


    If you write

    int a = 7;
    int b = 12;
    TBase *p = MyFunc(&a, &b);

    how is the compiler supposed to figure out the type of T or TBase? (The type
    of the return result cannot be used.)

    I think a template class may be what you need but it is hard to be sure from
    what you have given us.

    >
    > (the a and the b are used in some way in the true function)
    >
    > (clearly TBase must be a base class of T to make it work)
    >
    > I want to create something like
    >
    > list<pointerfunctions> a;


    If you have a list of objects, they must all be of the same type. However,
    they can be pointers (better yet smart pointers) to a common base class.
    Again, this suggests you really want a polymorphic class rather than a
    function here.

    >
    > How do I write the signature of the function? I can't use typedef because
    > there is a template...
    >
    > --- Thanks
    >
     
    Cy Edmunds, May 28, 2006
    #3
  4. MaxMax

    MaxMax Guest

    > Please elaborate on the restriction -- I don't see any reason why you
    > couldn't use typedef.
    > list< void (*)() > a;
    > /* Or alternatively */
    >
    > typedef void (*SimpleFunc)();
    >
    > list< SimpleFunc > b;

    Thanks a lot! I was finally able to do it! I had problems because I didn't
    remember how the () and the (*) needed to be used.

    --- bye
     
    MaxMax, May 28, 2006
    #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. Amadeus W. M.

    pointer to function template parameter

    Amadeus W. M., Sep 3, 2005, in forum: C++
    Replies:
    4
    Views:
    348
    Amadeus W. M.
    Sep 7, 2005
  2. Vijai Kalyan
    Replies:
    4
    Views:
    745
    Vijai Kalyan
    Nov 8, 2005
  3. ank
    Replies:
    4
    Views:
    1,156
    James Kanze
    Nov 1, 2006
  4. Stuart Redmann
    Replies:
    5
    Views:
    527
    Stuart Redmann
    Dec 14, 2007
  5. avasilev
    Replies:
    4
    Views:
    595
    avasilev
    Dec 22, 2011
Loading...

Share This Page