Function pointer returning a function pointer of the same kind

Started by
12 comments, last by Enigma 18 years, 6 months ago
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
Advertisement
No.

On so many levels.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
*** 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]
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
However, you can create a function object that accomplishes this.
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.
To expound upon what SiCrane said...
struct wtf{	wtf operator()(void) const	{		return wtf();	}};

What were you intending this for?
--Michael Fawcett
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();
}
I was just making an experiment, like, a main function that would look something like

main()
{

funcptr=menu();
while(funcptr) funcptr=(*funcptr)()

}

I saw the recursion, i just wondered if there was some kind of 'hack' to get around it :)

btw, how do you make a code-box on these forums?

This topic is closed to new replies.

Advertisement