private:
// Keeps track of the total number of entries in the table.
// Used to ensure we do not go over the limit. -1 means
// that we don't yet know how many entries the table has.
int num_total_entries_;全局变量:和数据成员一样,所有全局变量也要注释说明含义及用途.比如:
// The total number of tests cases that we run through in this regression test. const int kNumTestCases = 6;实现注释:对于代码中巧妙的,晦涩的,有趣的,重要的地方加以注释.
代码前注释:巧妙或复杂的代码段前要加注释.比如:
// Divide result by two, taking into account that x
// contains the carry from the add.
for (int i = 0; i < result->size(); i++) {
x = (x << 8) + (*result)[i];
(*result)[i] = x >> 1;
x &= 1;
}行注释:比较隐晦的地方要在行尾加入注释.在行尾空两格进行注释.比如:
// If we have enough memory, mmap the data portion too.
mmap_budget = max<int64>(0, mmap_budget - index_->length());
if (mmap_budget >= data_size_ && !MmapData(mmap_chunk_bytes, mlock))
return; // Error already logged.注意,这里用了两段注释分别描述这段代码的作用,和提示函数返回时错误已经被记入日志.
如果你需要连续进行多行注释,可以使之对齐获得更好的可读性:
DoSomething(); // Comment here so the comments line up.
DoSomethingElseThatIsLonger(); // Comment here so there are two spaces between
// the code and the comment.
{ // One space before comment when opening a new scope is allowed,
// thus the comment lines up with the following comments and code.
DoSomethingElse(); // Two spaces before line comments normally.
}NULL,true/false,1,2,3...:
向函数传入NULL,布尔值或整数时,要注释说明含义,或使用常量让代码望文知意.例如,对比:
Warning:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-35033-21.html
现在都搞互联网销售了
不如生活成本涨得快
放在家里不放心