Skip to content

Instantly share code, notes, and snippets.

@urbans0ft
Last active December 20, 2022 15:01
Show Gist options
  • Select an option

  • Save urbans0ft/5dc1e0e96d7592b406091e26c86d9c77 to your computer and use it in GitHub Desktop.

Select an option

Save urbans0ft/5dc1e0e96d7592b406091e26c86d9c77 to your computer and use it in GitHub Desktop.
C++ Style Guide

Living gist - not even close to be finished yet

Table of Contents

Files

Classes are to be delcared in header files (.h) and defined in .cpp files. The filenames must match the class name. To avoid multiple inclusions #pragma once is to be used as a guard in the first line of each class header file.

Naming

  1. Since class names ought to be in PascalCase their corresponding header and .cpp files must be in PascalCase as well.
  2. All other files must use camelCase.

Sequence of Inclusion

  1. The topmost #include in a .cpp file is its corresponding .h file, if there's one.
  2. C++ Standard Library
  3. System Libraries (e.g.: WIN32 API Header)
  4. 3rd Party Libraries
  5. Project Headers

Example:

// Foo.cpp
#include "Foo.h"

#include <string>
#include <iostream>

#include <Windows.h>
#include <winsock2.h>

#include "Bar.h"

Classes

Naming

Namespaces

Static Variables

Static Functions

Member Variables

  • Non public member variables start with an underscore ('_') followed by lower-case letter (camelCase).
  • Public member variables start with an upper-case letter (PascalCase)

Methods

Order of Declaration

#pragma once

#include <...>

#define DEBUG

namespace mynamspace 
{

using namespace othernamespace;

using cout;
using endl;

// defining custom types
using FunctionPtr = void (*)();

class Bar
{
  public:
    static const int PublicStaticConst = 0;
  protected:
    static const int ProtectedStaticConst = 0;
  private:
    static const int PrivateStaticConst = 0;
  
  public:
    static void PublicStaticFunction();
  protected:
    static void ProtectedStaticFunction();
  private:
    static void PrivateStaticFunction();
    
  public:
    int PublicInteger;
  protected:
    int _protectedInteger;
  private:
    int _privateInteger;
  
  public:
    Bar();                                // C'tor
    ~Bar();                               // D'tor
    Bar(const Bar& other);                // copy constructor
    Bar(Bar&& other) noexcept;            // move constructor
    Bar& operator=(const Bar& other);     // copy assignment
    Bar& operator=(Bar&& other) noexcept; // move assignment
    
    void publicMethod();

  protected:
    void protectedMethod();
    
  private:
    void privateMethod();
    
};
} // namespace mynamespace

Variables

More than one variable must not be declared per line. Constructs like this are strictly prohibited:

int a, *b, c = 0, *d = nullptr, **e;

The pointer * semantically belongs to the type. Thus the declaration above shell be written as:

int   a;
int*  b
int   c = 0
int*  d = nullptr
int** e;

Notice there's no gap between the pointer * and the type but between the * and the variable name. Variables may be indented.

References

To-do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment