Advertisement

Sub function in C++, ANSI hack?

Started by August 06, 2001 10:07 AM
1 comment, last by bobatefrei 23 years, 6 months ago
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
Advertisement
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.

This topic is closed to new replies.

Advertisement