两种类型的注释

类型的评论

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

规则:不要嵌套注释里面其他的评论

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值