C++primer的学习|section1.3. 关于注释


1.3. 关于注释

在程序变得更复杂之前,我们应该明白C++如何处理注释。注释可以帮助其他人阅读程序,通常用于概括算法、确认变量的用途或者阐明难以理解的代码段。注释并不会增加可执行程序的大小,编译器会忽略所有注释。

In this book, we italicize comments to make them stand out from the normal program text. In actual programs, whether comment text is distinguished from the text used for program code depends on the sophistication of the programming environment.

本书中,注释排成斜体以区别于一般程序文本。实际程序中,注释文本是否区别于程序代码文本取决于编程环境是否完善。

There are two kinds of comments in C++: single-line and paired. A single-line comment starts with a double slash (//). Everything to the right of the slashes on the current line is a comment and ignored by the compiler.

C++ 中有单行注释和成对注释两种类型的注释。单行注释以双斜线(//)开头,行中处于双斜线右边的内容是注释,被编译器忽略。

The other delimiter, the comment pair (/* */), is inherited from the C language. Such comments begin with a /* and end with the next */. The compiler treats everything that falls between the /* and */ as part of the comment:

另一种定界符,注释对(/* */),是从 C 语言继承过来的。这种注释以“/*”开头,以“*/”结尾。编译器把落入注释对“/**/”之间的内容作为注释:

   #include <iostream>
   /* Simple main function: Read two numbers and write their sum */
   int main()
   {
       // prompt user to enter two numbers
       std::cout << "Enter two numbers:" << std::endl;
       int v1, v2;           // uninitialized
       std::cin >> v1 >> v2; // read input
       return 0;
   }

A comment pair can be placed anywhere a tab, space, or newline is permitted. Comment pairs can span multiple lines of a program but are not required to do so. When a comment pair does span multiple lines, it is often a good idea to indicate visually that the inner lines are part of a multi-line comment. Our style is to begin each line in the comment with an asterisk, thus indicating that the entire range is part of a multi-line comment.

任何允许有制表符、空格或换行符的地方都允许放注释对。注释对可跨越程序的多行,但不是一定要如此。当注释跨越多行时,最好能直观地指明每一行都是注释。我们的风格是在注释的每一行以星号开始,指明整个范围是多行注释的一部分。

Programs typically contain a mixture of both comment forms. Comment pairs generally are used for multi-line explanations, whereas double slash comments tend to be used for half-line and single-line remarks.

程序通常混用两种注释形式。注释对一般用于多行解释,而双斜线注释则常用于半行或单行的标记。

Too many comments intermixed with the program code can obscure the code. It is usually best to place a comment block above the code it explains.

太多的注释混入程序代码可能会使代码难以理解,通常最好是将一个注释块放在所解释代码的上方。

Comments should be kept up to date as the code itself changes. Programmers expect comments to remain accurate and so believe them, even when other forms of system documentation are known to be out of date. An incorrect comment is worse than no comment at all because it may mislead a subsequent reader.

代码改变时,注释应与代码保持一致。程序员即使知道系统其他形式的文档已经过期,还是会信任注释,认为它会是正确的。错误的注释比没有注释更糟,因为它会误导后来者。

Comment Pairs Do Not Nest
注释对不可嵌套

A comment that begins with /* always ends with the next */. As a result, one comment pair cannot occur within another. The compiler error message(s) that result from this kind of program mistake can be mysterious and confusing. As an example, compile the following program on your system:

注释总是以 /* 开始并以 */ 结束。这意味着,一个注释对不能出现在另一个注释对中。由注释对嵌套导致的编译器错误信息容易使人迷惑。例如,在你的系统上编译下面的程序:

   #include <iostream>
   /*
    * comment pairs /* */ cannot nest.
    * "cannot nest" is considered source code,
    * as is the rest of the program
    */
   int main()
   {
       return 0;
   }

When commenting out a large section of a program, it can seem easiest to put a comment pair around a region that you want to omit temporarily. The trouble is that if that code already has a comment pair, then the newly inserted comment will terminate prematurely. A better way to temporarily ignore a section of code is to use your editor to insert single-line comment at the beginning of each line of code you want to ignore. That way, you need not worry about whether the code you are commenting out already contains a comment pair.

当注释掉程序的一大部分时,似乎最简单的办法就是在要临时忽略的区域前后放一个注释对。问题是如果那段代码已经有了注释对,那么新插入的注释对将提前终止。临时忽略一段代码更好的方法,是用编辑器在要忽略的每一行代码前面插入单行注释。这样,你就无需担心要注释的代码是否已包含注释对。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值