Like this...
Code:
void foo2(int);
void foo(int n)
{
typedef void (myptr)(int);
myptr* myptr_ = &foo2;
myptr_(n);
}
// Since foo has a static type as argument, we know what type foo2 must take.
// No need for any templates here.
void foo2(int) { }
Example 2:
Code:
void foo2(void*);
void foo(void* n)
{
typedef void (myptr)(void*);
myptr* myptr_ = &foo2;
myptr_(n); // What type is n???
}
// Since foo has a dynamic type, it's impossible for us to determine what type foo2 should
// take. Therefore, it will probably take the same type as foo, because foo cannot easily
// determine what type it has taken and what foo2 should take in that case.
void foo2(void*) { }
Therefore, a more viable solution are template functions:
Code:
template<typename T> void foo2(T);
template<typename T> void foo(T n)
{
// We know now that foo2 must take an argument of T, so we get a specialized
// function pointer.
typedef void (myptr)(T);
myptr* myptr_ = &foo2<T>;
// We now know the type of T. If we wanted to do some template-programming, we could
// easily created specialized functions and create appropriate function pointers.
myptr_(n);
}
template<typename T> void foo2(T) { }
Does this make sense?