pointer to function as template parameter

M

MaxMax

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
 
T

Tomás

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
 
C

Cy Edmunds

MaxMax said:
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.
 
M

MaxMax

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top