Sub function in C++, ANSI hack?
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
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
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
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.

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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement