2017-02-17 55 views
4

说我有下面的代码短(在C++中,但是这可能不是重要的问题):的Eclipse:注释整个代码,而它得到削减其他评论

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

在Eclipse,当我想通过在代码前后加上/ *和* /来注释掉整个代码,注释在第3行的“This is a comment”的末尾由* /截断,所以剩下的代码被留下注释。

/* //<--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 

任何人都知道解决这个问题的方法,或者我只需要处理它或使用//注释...?

回答

6

在C++中没有嵌套注释的方法。一种解决方案(特别是如果你不想改变很多/* *///)是使用预处理器,你可以这样做

#ifdef SOME_RANDOM_SYMBOL 

code that you want to comment here 

#endif 

只要确保SOME_RANDOM_SYMBOL不以某种方式在你的代码中定义。

正如在评论中提及的@Caleb,你也可以做

#if 0 

code that you want to comment here 

#endif 

但使用符号允许你搜索。

+1

或者只是'#if 0 ...#endif',但是'SOME_RANDOM_SYMBOL'允许您在想要撤消更改时为其提供一个易于搜索的名称。 – Caleb

+0

@Caleb是的,好点! – vsoftco

+0

太棒了,谢谢! – Svavinsky