4

Say I have the following code (in C++, but that's probably not important to the question):

int main() {
    ....random code....
    /*This is a comment*/
    ....random code....
    return 0;
}

In eclipse, when I want to comment out the entire code by putting /* and */ before and after the code, the comment is cut short by the */ at the end of the "This is a comment" on line 3, so the rest of the code is left uncommented.

/*    //<--overall comment starts here
int main() {
    ....random code....
    /*This is a comment*/    //<--overall comment ends here
    ....random code....
    return 0;
}
*/  //<--overall comment SHOULD end here

Anyone know a way of getting around this problem, or do I just have to deal with it or use // comments...?

Svavinsky
  • 69
  • 3

1 Answers1

6

There's no way of having nested comments in C++. One solution (especially if you don't want to change lots of /* */ to //) is to use the pre-processor, you can do something like

#ifdef SOME_RANDOM_SYMBOL

code that you want to comment here

#endif

Just make sure that SOME_RANDOM_SYMBOL is not somehow defined in your code.

As mentioned by @Caleb in the comment, you can also do

#if 0

code that you want to comment here

#endif

but using a symbol allows you to search for it.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 1
    Or just `#if 0 ... #endif`, but `SOME_RANDOM_SYMBOL` allows you to give it an easily searchable name for when you want to undo the change. – Caleb Feb 17 '17 at 17:51