C++ coding standards
This document contains a suggestion of C++ coding standards. This is a combination of other standards and is roughly what I follow when I code.
Obviously an incomplete document, but I'll add stuff soon :)
Naming files
All header files should follow the example:
ThisIsMyFile.hpp
Corresponding source files should follow the example:
ThisIsMyFile.cpp
The .cpp and .hpp files can be in the same path if you like (recommended for small projects), however the build system should be separate (no object files should appear in the source code during compilation). I personally use CMake for building projects.
Comments
Doxygen is always a good idea!
- Documenting header files
/** This is a very nice function */
int function(void); - Documenting source files
int myVariable = myFunctionCall(1); // initializing a variable
Naming functions
- Definition outside of a class
bool getStoredValue(void);
Always start with lower case. For every word, use upper case to mark the beginning of a word.
- Definition within a class
- Non-static member functions
bool getStoredValue(void);
- Static member functions
bool GetStoredValue(void);
The difference is that static function names start with upper case.
- Non-static member functions
Naming variables
- Global Variables
It is in general good to avoid global variables. However, if they are really needed, they should be names as:
int ThisIsAGlobalVariable
- Member variables
- Public
Public member variables should be avoided, but if it is essential they are public, the naming should be as follows:
int ThisIsAPublicVariable
or
int thisIsAPublicVariable
- Non-public
int m_thisIsAPrivateVariable
- Public
Defining macros
Defining a macro should look like:
#define MYMACRO my_macro_code