Program style - C ++ style guide

xiaoxiao2021-03-05  24

Program style - C style guide Original: Neill Kipp Translation: Panic March 30, 2005 Translating: This is a article written on January 23, 1996, now has 9 years, very Old, it is possible to keep up with the situation, but some things are still worth learning, I translated this text is for readers only. Original link:

http://www.gamedev.net/reference/articles/Article708.asp

The file header file has ".h" suffix. The header file contains classes, structures, and union declarations, enumeration (enum) declarations, # define, typedef. The implementation file has a ".cc" (unix) or ".cpp" (Windows, DOS) suffix. Implementation files include functions and methods. Arrange a header in the header file and source code file. The header can contain information about the title, authors, date, and some projects, such as this file is coordinated with the entire project.

Some names of the name General C characters: (Note: These are the original name of the symbol, there is no fully standardized Chinese vocabulary, so the back translation is just personal suggestions) {Open Brace, Open curly left flower brackets} Close Brace, Close Curly Right Brand (Open Parenthesis, Open Paren) Close Parenthesis, Close Paren Right Brand Brand [Open Bracket] Close Bracket Right Brand. Period, DOT Junction, Point ! Quote, take the address * Asterisk, Multiply, Star, Pointer asterisk, Multiply, Star, Pointer / Slash, Divide diagonal, Semi // Slash-Slash, Comment double slash,

Note # Pound Well (Macro: #, the reference to translate the symbol to the string of macro skills) / backslash, (Sometimes "escape") anti-sliding line, (sometimes doing escape) (there is also one: Continue符) ~ TILDE The basic type "char" in place "char" is usually pronounced the first syllable of "charcoal.".

Sometimes the name "CARE" or "car." Name and typeset name in INTERSPERSED_UNDERSCORES Intermediate Underline LowerCaseMixedCapital lowercase mix (first letter) uppercase; CapitalMixedCapital (first letter) uppercase mix (first letter) uppercase; All_uppercase all capitalization naming apparatus Enumeration_item_name enumeration, lowercase and overline; VariableName variable, lowercase prefix add first letters overrup hybrid; type name, class name, method name, first letters overrup; UNIXFileName.cc Unix / L Inux file name: each word first letter uppercase; dosfn.cpp windows / dos file name: all lowercase; Pound_Defines macro definition, all uppercase; self-made file code (that is, there is no document, only the source code file for notes and code description The program uses a complete spelling for each name. Avoid direct use of numbers, not allowed to occur in addition to 0 (

Sometimes including 1) digital constant. Use constant variables or macro definitions (#defines). Blank space (get the space bar); new row (press Enter key); Tab (TAB) (Tab) Spacer instead; after blank and plated left flower parentheses, 4 spaces are retracted until the corresponding right ribbon appears .; if IF, while, or the for does not follow the curly brackets, the next line is indented into two spaces; The statement ended in the colon, indent the two spaces (PUBLIC, CASE); reserved words (if, else, class, struct) to add 1 holiday unless it has been indented by the new row or special punctuation; operator There is a space before and after the comparative character; except! The pointer variable (&, *) is in the expression, before (not behind) to add a space LLOWED); add a space after the left brackets; the left flower parentheses after the next these keywords should be wrapped after parentheses: CLASS, Struct, Union, ENUM, METHOD, FUNCTI ON (rather than: IF, else, do, for, while, Switch --- these rough numbers is only one space, "Method, function, IF , else, do, for, while, Switch's right flowers should be wrap; Class, Struct, union's right ribos, wrap and insert new space lines.

(Originally written Semi-Colon, does not understand the meaning); left flower parentheses to change. Note Note always "//" from the current indentation "//" and then pass one space; other comments are not allowed in the comment; After the object of the comment. (Translator Note: Original Comments Always Preceed The Construct T Hey Address); Use the full statement in the comment; when used to declare, the comment can use the prayer sentence; above, it is your code looks comfortable. Guide, is also your code more readable guide. Summer file example: // Module Name: classname.h // provides: 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 incrudes another class decaration. #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 spacs. // 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 value (); classname ();

// The destructor Guarantees Clean Deallocation. // The Tilde (~) IT IT ITHOD 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 Source Code File Example:

// 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 incrudes 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;

} ////////////////////////////////////////////////////////////////////////////// ' . 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 Ove Rflow. cout << "no overflow occurred./N"; Break; Case overflow_occurred: // Display Message That Overflow Occurred. Cout << "Warning: Oveflow Occurred./N"; BREAK

转载请注明原文地址:https://www.9cbs.com/read-36300.html

New Post(0)