View Full Version: blocking off sections of code

C++ Learning Community > C++ Tips > blocking off sections of code


Title: blocking off sections of code
Description: a little trick


nickeax - December 24, 2003 12:52 AM (GMT)
Gday. If you are testing some code and want to block out a whole slab, then enable it again, then block it etc, you can employ this trick I thought of. (Maybe it's used all the time, but I haven't seen it anywhere)

Place //*/ at the end of the block you wish to hide. When you want to hide it,
simply place /* at the start of the block. It means you only have to worry about the starting comment.

Come to think of it... It's kind of like how they put some JavaScript in to hide it from old browsers.. Anyway, I hope someone finds this tip handy!




Nick.

ih8censorship - December 24, 2003 12:54 AM (GMT)
hey i never thought of that now that i think of it its a pretty good idea!

TheHawgMaster - December 24, 2003 03:29 AM (GMT)
Heh I love tricks like that :) My personal pet is the multidirectional comment:
CODE
/*/

nickeax - December 24, 2003 03:33 AM (GMT)
Phew...! I kind of thought I'd get shouted down in a hail of
EVERYONE knows that... You DIDN'T think of it... lol...

This forum has nice people that visit it! Sometimes programmers are not real nice... :)

ih8censorship - December 24, 2003 03:37 AM (GMT)
QUOTE
This forum has nice people that visit it! Sometimes programmers are not real nice...


yeah i have noticed that too i think ego has a big thing to do with it.... i think the reason we dont have it here so much is because everyone is learning and they talk how they want to be talked to. i have flamed a few people, but they deserved it ;)

TheHawgMaster - December 24, 2003 03:45 AM (GMT)
It could also have to do with everyone here being teenagers :lol:

AcidGame - December 27, 2003 11:44 AM (GMT)
Thats a good idea, =P

-

multidirectional comments scare me. :-X

/*/ lol

myork - January 21, 2004 03:58 PM (GMT)
The one problem with using comments to block of sections of code is that sometimes they don't work if the block has another embedded comment.

CODE


/* Comment out Block for debugging.
for(int loop=0;loop < max;loop++)
{
  /*Print out the values in the array*/
  std::cout << array[loop] << '\n';
}
*/



This is obviously a trivial example and can be made to work by changing the comment to C++ style comment. But commenting out large sections may require a lot of changes.


An alternative method is to use the pre-preocessor.

CODE

#if 0  // Commented out Block for debugging

for(int loop=0;loop < max;loop++)
{
  /*Print out the values in the array*/
  std::cout << array[loop] << '\n';
}

#endif


To turn it back on just replace the 0 with a 1.




* Hosted for free by InvisionFree