circular function pointers declaration?

T

Timothee Groleau

Hi,

Is it possible to declare a function pointer whose arguments is a pointer to
a function pointer of the same type? Something like:

=====
typedef unsigned long (*myFuncPtr) (myFuncPtr*);
=====

The above is not working and generates the error below:
=====
test.cpp:5: error: `myFuncPtr' was not declared in this scope
test.cpp:5: error: syntax error before `)' token
=====

Is there a way to declare a dummy myFuncPtr earlier so I can use the name as
an argument?

Thanks!
Tim.
 
B

benben

Timothee said:
Hi,

Is it possible to declare a function pointer whose arguments is a pointer to
a function pointer of the same type? Something like:

=====
typedef unsigned long (*myFuncPtr) (myFuncPtr*);
=====

OPTION A
----------------------------------------------
Simply:

typedef unsigned long (*myFuncPtr) (void*);

Just be careful to cast your pointers to void*.


OPTION B
----------------------------------------------
Abstract base class approach:

class my_itf
{
public:
virtual unsigned long
operator()(my_itf& pnext) = 0;
};

template <unsigned long N>
class my_itf_end: public my_itf
{
public:
unsigned long operator()(my_itf&)
{
return N;
}
};

which can be used like:

class A: public my_itf
{
public:
unsigned long operator()(my_itf& next)
{
return next(my_itf_end<5>());
}
};

int main()
{
A a;
my_itf& f = a;

f(f);
}

Pick your poison :)

Regards,
Ben
 
M

Michiel.Salters

Timothee said:
Hi,

Is it possible to declare a function pointer whose arguments is a pointer to
a function pointer of the same type?

No, this was a Guru Of The Week question. (www.gotw.ca)

HTH,
Michiel Salters
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top