1.3 C++程序的注释
 
注释是为了帮助其他人读懂你的程序。注释一般用来简要概述一个算法,标明一个变量的用途,或者解释一段不容易读懂的程序段。注释会在被编译器忽略,所以不会增加可执行程序的大小。

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;
   }

在使用多行注释时,我们把每一行前面都加上*,这样能更清楚地标示出每一行注释,像下面这样:
/*
 * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
 * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 *
 * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
 *
 */

当程序改变时,一定要注意修改相应的注释。

多行注释不能嵌套使用,像“/*this is /*illegal*/ form*/”这样写是不行的。

如果我们想暂时把一段程序除去,可以用多行注释把它们注释掉,但如果这段程序中已经用了多行注释,那就不行了。更好的办法是在每行前面用“//”单行注释,这样就完全不会出错了。