日期:2011-03-22 13:55:00 来源:本站整理
<b>程序气势的要素-C++气势指南</b>[VC/C++编程]
本文“<b>程序气势的要素-C++气势指南</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
译者序:这是一篇写于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."
名字和排版
命名约定的名字
命名约定的利用
自成档代码(也就是没有文档,仅靠注释和代码阐明的源代码文件)
避免直接利用数字(Magic number)
空白
空白和排版
换行
注释
头文件示例:
// 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
源代码文件示例:// 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.
";
break;
case overflow_occurred:
// Display message that overflow occurred.
cout << "Warning: overflow occurred.
";
break;
}
}
其他例子:// 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);
}
以上是“<b>程序气势的要素-C++气势指南</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论