Archived

This topic is now archived and is closed to further replies.

bobatefrei

Sub function in C++, ANSI hack?

Recommended Posts

bobatefrei    122
Is this code compatible with ANSI C++? Witch compiler support it? void MyFunction() { struct Local { static void RecursiveFunction(int i) { if (i > 0) RecursiveFunction(i-1); } }; Local::RecursiveFunction(6); } PS: I found it on www.flipcode.com

Share this post


Link to post
Share on other sites
Dactylos    122
It looks pretty ANSI compatible, and GCC/MinGW32 2.95.2 has no complains about it.

There's no real point to it though. It's not a real "sub-function" (don't remember what they're really called) like those you can write in for example Pascal, since Local::RecursiveFunction won't be able to access any variables that are local to MyFunction. A 'true' "sub-function" should be able to access data in the syntactically enclosing function, through the stack frame pointers.

Edited by - Dactylos on August 6, 2001 11:28:53 AM

Share this post


Link to post
Share on other sites
merlin9x9    174
Yeah, it''s perfectly ANSI.

I happen to think that such things are useful sometimes when you need to perform a repetitive task in a function and want to make it obvious that the task is only used there. The alternative is to use a macro or a separate function declared static. Just like goto, this kind of trickery has its place.

Share this post


Link to post
Share on other sites