C++ style guide: Difference between revisions

Jump to navigation Jump to search
1,188 bytes added ,  6 September 2021
Add separator recommendation between member functions and variables.
m (Better example for const reference usage)
(Add separator recommendation between member functions and variables.)
(15 intermediate revisions by the same user not shown)
Line 54: Line 54:
</syntaxhighlight>
</syntaxhighlight>


=== Non indenting whitespace ===
==== Non-indenting whitespace ====


Consider putting extra braces around a multi-line expression to make it
Consider putting extra braces around a multi-line expression to make it
Line 60: Line 60:
put extra braces anywhere if it improves clarity.
put extra braces anywhere if it improves clarity.


The negation operator is written with a space between the operator
=== Pointer and Reference appearance ===
and its target, e.g., {{codeline|! A}}.


Declarations of pointers have the '*' character cuddled with the name of the variable.
Declarations of pointers have the '*' character cuddled with the name of the variable.
Line 74: Line 73:
unsigned int& reference_variable;
unsigned int& reference_variable;
</syntaxhighlight>
</syntaxhighlight>
=== Miscellaneous ===
The negation operator is written with a space between the operator
and its target, e.g., {{codeline|! A}}.


=== Function headers ===
=== Function headers ===
Line 90: Line 94:
parenthesis.  You should put a space before the left open parenthesis and after
parenthesis.  You should put a space before the left open parenthesis and after
commas, for both function definitions and function calls.
commas, for both function definitions and function calls.
=== Class declarations ===
The access specifier ({{codeline|public}}, {{codeline|protected}}, {{codeline|private}}) should always be stated rather than relying on the C++ language defaults for a particular object (for example, "{{codeline|class}}" = "{{codeline|private}}").
Within a class, the different access blocks should appear in the order 1) {{codeline|public}}, 2) {{codeline|protected}}, 3) {{codeline|private}}.
Within an access block, member functions should be specified before member variables.  If there are both member functions and member variables use
    //--------
between the sections to visually separate the two categories.


=== Namespace ===
=== Namespace ===
Line 139: Line 155:
names consisting of 1-2 letters.  Do not use mixed case names.
names consisting of 1-2 letters.  Do not use mixed case names.


=== Member Variables ===
Member variables should use the prefix "m_" whenever possible.
=== Class Variables ===
Class variables should use the prefix "s_" (for "static") whenever possible.
=== Filenames ===
As with m-files, the file name of a C++ source file containing a class should match the name of the class defined within the file.  For example, "password.h" defines the class "password" rather than "passwd.h" which is a common abbreviation for "password".


== Header Files ==
== Header Files ==
1,072

edits

Navigation menu