C + + Naming conventions

Source: Internet
Author: User
Tags naming convention variable scope

Goto: Original link

Http://blog.sina.com.cn/s/blog_a3a8d0b1010100uw.html

I. Classes and Interfaces

1 . Naming: Class names begin with the capital letter "C" followed by one or more words. Capitalize the first letter of each word. The interface begins with an uppercase "I" and represents the interface.

2 . Form of composition: recommend the form of "noun" or "adjective + noun", for example: "Canalyzer", "Cfastvector" ....

Second, function

1 . Naming: The name of a function consists of one or more words. Capitalize the first letter of each word. The maximum length of the 20 characters is not exceeded.

2 . Form: The global function should use the form of "verb" or "verb + noun" (dynamic-object phrase). For example: "GetName ()", "Drawbox ()".

The class member function should use only "verbs", and the omitted noun is the object itself. For example:

"Box->draw ();".

3 . Global function: Start with the lowercase prefix "g" or "M".

4, back harmonic event handler function: The habit begins with the word "on". For example: "_ontimer ()", "OnExit ()"

Third, variable

Variables are the most frequently used identifiers in a program, and the naming conventions of variables are the most important part of a set of C + + naming conventions:

1 . Name: The variable name consists of the scope prefix + type prefix + one or more words. A variable is combined with a word that starts with a lowercase letter, and the first letter of the second word is capitalized. For example: int ndrawmode. The variable cannot be longer than 20 characters.

Special: For some simple and straightforward local variables, you can also use simplified methods such as: I, J, K, X, Y, Z

2, form: The name of the variable should use "noun" or "adjective + noun". For example: "NCode", "M_nstate", "Nmaxwidth", "OldValue", "NewValue".

3 . Scope prefix: The scope prefix indicates the visible range of a variable. There are several scopes that can be used:

Prefix

Description

Example

No

Local variables

M_

Member variable for Class (member)

Int M_width

Ms_

Static member variable of class (static member)

static int ms_initvalue;

S_

Static variable (statics)

static int s_initvalue;

G_

External global Variables (GLOBALS)

int g_howmanypeople;

Sg_

Static global variables (statically global)

Gg_

Shared data segment global variables shared between processes (World global)

Description: The scope prefix is different from the following type prefix and should be enforced resolutely. The reasons are:

1) Variable scope and link changes are rare, for example, in rare cases a member variable is changed to a static variable

2) tools used in programming often do not visually display the scope and linkage of variables

4 . Type prefix: The scope prefix indicates the visible range of a variable. The type prefix indicates the type of a variable, as follows:

Prefix

Description

Example

B

Boolean variable (bool, BOOL)

benable

Ch

Character variable (char TCHAR)

Chname

Lpsz

LPSTR, LPCSTR, LPCTSTR

LpszName

N

Integer and bit-field variables (int, uint,__int32,__int64)

Nlength

L

Long

Loffset

By

BYTE

W

Word

Wpos

Dw

DWORD

Dwrange

F

Floating-point variable (float)

D

Double

P

Pointer-type variables and iterations (pointer)

PDoc

Lp

Far pointer

E

enum-type variable (enumeration)

Pfn

Pointer variables and function object pointers, especially for pointers to functions (pointer of function)

G

Array (GRID)

H

Handle Windows Object handle

HWnd

Iv. Constants

Constant names are made up of all uppercase letters, and the words are defined by underscores, such as: DELIMITER, Max_buffer. The definition of a type prefix is the same as in a variable naming convention.

Structure, macro, enumeration variable, union

All are made up of prefix + uppercase letters, with underscores defined between words.

1 . struct: Add lowercase prefix "_" or "__", then start with uppercase letters.

Cases:

typedef struct _POINT

{

int x;

int y;

} point;

2, macro: The composition of capital letters, the use of underline between the words to define

Example: #define MAXNUMBER 100

3 . Enumeration variable: Add lowercase prefix "enum".

Cases:

typedef enum _FILE_OPEN_MODE

{

Open_readonly,

Open_readwrite

}file_open_mode;

C + + Naming conventions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.