Function pointer returning a function pointer of the same kind
Hiya all. I am wondering, is it possible to make a function pointer that returns another function pointer of the same kind(the returned function pointer returns a function pointer of the same kind and so on) and how do you do it?
Thanks
No, that's a circular definition.
You're trying to tell the compiler to create a new type with only the information that the new tpye is equal to itself.
What are you trying to accomlish by this? We can probably work out a solution if we know what problem you're trying to solve.
You're trying to tell the compiler to create a new type with only the information that the new tpye is equal to itself.
What are you trying to accomlish by this? We can probably work out a solution if we know what problem you're trying to solve.
*** absolute qualifier snipped ***
EDIT: Apparently you can, but not with regular function pointer syntax. (see Enigma's link)
[Edited by - stylin on October 5, 2005 9:31:19 AM]
EDIT: Apparently you can, but not with regular function pointer syntax. (see Enigma's link)
[Edited by - stylin on October 5, 2005 9:31:19 AM]
Or you could always throw away type-safety and return it as a void pointer.
I've used this kind of design (with a slightly different method) for a parser state machine.
I've used this kind of design (with a slightly different method) for a parser state machine.
To expound upon what SiCrane said...
What were you intending this for?
struct wtf{ wtf operator()(void) const { return wtf(); }};
What were you intending this for?
struct FuncPtr_;typedef FuncPtr_ (*FuncPtr)();struct FuncPtr_{ FuncPtr_(FuncPtr pp) : p(pp) { } operator FuncPtr() { return p; } FuncPtr p;};FuncPtr_ f(){ return f; // natural return syntax}int main(){ FuncPtr p = f(); // natural usage syntax p();}
(Thanks Herb).
Enigma
#include <stdio.h>
#include <conio.h>
#include <windows.h>
typedef void* (*fp) ();
void* g();
void* f()
{
printf("f\n");
Sleep(100);
return g;
}
void* g()
{
printf("g\n");
Sleep(100);
return f;
}
void main()
{
fp r=(fp)f;
while(!kbhit()) r=(fp)r();
}
#include <conio.h>
#include <windows.h>
typedef void* (*fp) ();
void* g();
void* f()
{
printf("f\n");
Sleep(100);
return g;
}
void* g()
{
printf("g\n");
Sleep(100);
return f;
}
void main()
{
fp r=(fp)f;
while(!kbhit()) r=(fp)r();
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement