措施气势气魄的要素-C++气势气魄指南
当前位置:以往代写 > C/C++ 教程 >措施气势气魄的要素-C++气势气魄指南
2019-06-13

措施气势气魄的要素-C++气势气魄指南

措施气势气魄的要素-C++气势气魄指南

副标题#e#

译者序:这是一篇写于1996年1月23日的文章,到此刻已经有9个年初了,很陈旧,有大概跟不上形势,可是有些对象仍然值得此刻的开拓者进修,我翻译这篇文字仅供读者参考。

原文链接:http://www.gamedev.net/reference/articles/article708.asp

文件

头文件有".h"后缀。头文件包括类(class),布局(struct),和连系(union)的声明,列举(enum)的声明,#define,typedef。

实现文件有一个".cc" (UNIX) 可能".cpp" (Windows, DOS)后缀。实现文件包罗函数和要领的实现。

在头文件和源代码文件中布置一个页眉。页眉可以包括标题,作者,日期,和一些工程的信息,好比这个文件是共同整个工程的。

一些名字

通用C++字符的名字:

(注:这些都是标记的英文原名,今朝并没有完全尺度化的汉语词汇对应,所今后头的翻译只是小我私家发起)

{ open brace, open curly左花括号
} close brace, close curly 右花括号
( open parenthesis, open paren 左圆括号
) close parenthesis, close paren 右圆括号
[ open bracket 左方括号
] close bracket 右方括号
. period, dot 句号,点
! exclamation point, bang, not 叹号,否
| bar, vertical-bar, or, or-bar (actually a "vertical virgule") 竖线,按位或
& ampersand, and, reference, ref 和,按位与,引用,取地点
* asterisk, multiply, star, pointer 星号,乘号,星,指针
/ slash, divide 斜线,除号
// slash-slash, comment 双斜线,注释符
# pound 井号 (宏:#,参考 把标记转化为字符串的宏能力)
\ backslash, (sometimes "escape") 反斜线,(有时候做转义符)(尚有一个:续行符)
~ tilde 按位取反

根基范例 "char" 凡是发音是"charcoal."的首音节。有时念作 "care" 可能 "car."

名字和排版

定名约定的名字

interspersed_underscores 中间下划线

lowercaseMixedCapital 小写殽杂(首字母)大写;

CapitalMixedCapital (首字母)大写殽杂(首字母)大写;

ALL_UPPERCASE 全部大写

定名约定的应用

enumeration_item_name 列举,小写加下划线;

variableName 变量,小写前缀加首字母大写后缀;

TypeName, ClassName, MethodName() 范例名,类名,要领名,首字母大写前后缀;

UnixFileName.cc Unix/Linux文件名:每个单词首字母大写;

dosfn.cpp windows/dos文件名:全部小写;

POUND_DEFINES 宏界说,全部大写;

自成档代码(也就是没有文档,仅靠注释和代码说明的源代码文件)

措施中为每个名字利用完整拼写.

制止直接利用数字(Magic number)

不答允呈现除了0(有时也包罗1)之外的数字常量. 利用常变量可能宏界说(#defines).

空缺

空格(按空格键获得) ;

新行(按回车键获得) ;

制表符(tab) (用8个空格取代) ;

空缺和排版

左花括号之后, 每行缩进4个空格直到对应的右花括号呈现.;

假如if, while, 或 for 后头没有跟花括号, 下一行缩进两个空格;

冒号末了的语句,反向缩进两个空格(public, case);

保存字(if, else, class, struct) 前后要加1个空格除非已经因为新行可能非凡标点做了缩进;

运算符和较量符前后要有一个空格 (除了!之外);

指针变量 (&,*) 声明的时候要前后加一个空格;

指针变量 (&,*) 在表达式中,前面(不是后头)要加一个空格llowed) ;

左圆括号后要加一个空格;


#p#副标题#e#

换行

在下面这些要害字后的左花括号后要换行: class, struct, union, enum, method, function (而不是: if, else, do, for, while, switch — 这些的花括号后只要1个空格.)

要领(method),函数( function), if, else, do, for, while, switch的右花括号后要换行;

class, struct, union的右花括号后要换行并插入新空行。.(原文有写Semi-colon,不领略寄义);

左花括号后要换行.

注释

注释老是从当前缩进开始 "//" 然后紧接一个空格;

注释中不答允其他注释;

注释要加在注释的工具之后. (译者注:原文 Comments always preceed the construct they address );

注释中利用完整语句;

用于声明的时候,注释可以利用祈使句; 上面这些,是你的代码看起来舒服的指南,也是你的代码更具可读性的指南.

头文件示例:

#p#分页标题#e#

// MODULE NAME: ClassName.h
//   PROJECT: CS1344-1,2 Course Notes
//    AUTHOR: Neill Kipp
//     DATE: January 1, 1996
// DESCRIPTION: This file presents examples of naming and
// indentation style in a C++ class declaration. This title
// information is minimal.
// The following prevents files from being included
// twice. It is a naming exception designed to emulate a file name
// (period is not a name character; underscore is).
#ifndef ClassName_h
#define ClassName_h
// This directive includes the superclass declaration.
#include "super.h"
// This directive includes another class declaration.
#include "other.h"
// The comment for an enumeration declaration precedes the declaration.
enum OverflowState
{
   // Each item''s comment precedes it at the same indentation as the item.
   no_overflow,
  
   // Follow the last item with a comma;
   // it helps avoid syntax errors when adding or rearranging items.
   overflow_occurred,
};
// This class shows how naming conventions and comments are used in a
// simple class declaration. Whitespace precedes and follows reserved
// words (like "public").
class ClassName
{
   // After a brace, indent four spaces.
   // The description of the variable "memberData" goes here.
   int memberData;
   // If a line ends in single colon, reverse-indent two spaces.
  public:
   // The constructor gives initial values to member data.
   ClassName();
   // The destructor guarantees clean deallocation.
   // The tilde (~) is part of the method name. It is not an operator.
   ~ClassName();
   // This method increments the member variable by the value in "howMuch"
   // and returns TRUE if overflow is detected (FALSE otherwise). Method
   // comments tell what the method does, what the arguments are,
   // and what the method returns.
   OverflowState IncrementMemberVariable( int howMuch);
  
   // Prints message about overflow.
   void ShowOverflow( OverflowState overflow);
};
#endif

#p#副标题#e#

源代码文件示例:

// MODULE NAME: ClassName.cc
//   PROJECT: CS1344-1,2 Course Notes
//    AUTHOR: Neill Kipp
//     DATE: January 1, 1996
// DESCRIPTION: This file presents examples of naming and
// indentation style in a C++ class implementation. This title
// information is minimal.
// This directive includes header information for the "ClassName" class.
#include "ClassName.h"
ClassName::ClassName()
{
   // Initialize member data (statement comments are in the imperative,
   // and preceed the statement). Suggestion: write the comments first, then
   // write the code.
   memberData = 0;
}
// The return type appears on the first line,
// followed by the class name colon-colon on the second,
// and finally the method name on the last. Then a newline, an open brace
// and then indent. Notice the space after the open parenthesis. It helps
// the eye catch the type name.
OverflowState
ClassName::IncrementMemberVariable( int howMuch)
{
   // Check the overflow condition.
   if ( TOO_BIG - memberVariable > howMuch) {
// If overflow, return that overflow occurred.
return overflow_occurred;
   } else {
// Otherwise, return overflow is ok.
return overflow_none;
   }
}
// This code implements the ShowOverflow method.
void
ClassName::ShowOverflow( OverflowState overflow)
{
   // Switch is a reserved word. It is followed by a space.
   switch ( overflow) {
// Lines ending in a colon reverse indent two spaces.
    case no_overflow:
// Display message about no overflow.
cout << "No overflow occurred.\n";
break;
    case overflow_occurred:
// Display message that overflow occurred.
cout << "Warning: overflow occurred.\n";
break;
   }
}

其他例子:

#p#分页标题#e#

// Note the spacing and indentation in the for statement.
for ( whichItem = 0; whichItem < BIG_NUMBER; whichItem++) {
   DoSomething( whichItem);
}
// Bang is not followed by a space.
while ( !SemaphoreOK()) {
   DoWaitForSemaphore( LONG_TIME);
}

    关键字:

在线提交作业