类型的评论
A comment is a line (or multiple lines) of text that are inserted into the source code to explain what the code is doing. In C++ there are two kinds of comments.
注释是一个线(或多线)的文本,插入到源代码解释代码是做什么的。在C++中有两种类型的注释。
The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to the end of the line. For example:
/ /符号开始一个C++单行注释,它告诉编译器忽略一切到终点。比如说呢。
1
|
cout <<
"Hello world!"
<< endl;
// Everything from here to the right is ignored.
|
Typically, the single line comment is used to make a quick comment about a single line of code.
1
2
3
|
cout <<
"Hello world!"
<< endl;
// cout and endl live in the iostream library
cout <<
"It is very nice to meet you!"
<< endl;
// these comments make the code hard to read
cout <<
"Yeah!"
<< endl;
// especially when lines are different lengths
|
Having comments to the right of a line can make the both the code and the comment hard to read, particularly if the line is long. Consequently, the // comment is often placed above the line it is commenting.
1
2
3
4
5
6
7
8
|
// cout and endl live in the iostream library
cout <<
"Hello world!"
<< endl;
// this is much easier to read
cout <<
"It is very nice to meet you!"
<< endl;
// don't you think so?
cout <<
"Yeah!"
<< endl;
|
The /*
and */
pair of symbols denotes a C-style multi-line comment. Everything in between the symbols is ignored.
1
2
3
|
/* This is a multi-line comment.
This line will be ignored.
So will this one. */
|
Since everything between the symbols is ignored, you will sometimes see programmers “beautify” their multiline comments:
1
2
3
4
|
/* This is a multi-line comment.
* the matching asterisks to the left
* can make this easier to read
*/
|
Multi-line style comments do not nest. Consequently, the following will have unexpected results:
1
2
|
/* This is a multiline /* comment */
this
is not inside the comment */
// ^ comment ends here
|
规则:不要嵌套注释里面其他的评论