Show Menu
Cheatography

A cheatsheet for C++

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Address of Operator

int x = 42;
int* ptr = &x;  // ptr holds the memory address of x

Template Aliases

// The template
template <typename T>
class Box {
    T contents;
};

// Without an alias — verbose
Box<int> myBox;

// The alias
using IntBox = Box<int>;

// With the alias — cleaner, same thing
IntBox myBox;
 

Reference declar­ation (in type declar­ations)

int x = 42;
int& ref = x;  // ref IS x — same memory location

ref = 100;     // x is now 100
 

Template Functions

template <typename T>
T add(T a, T b) {
    return a + b;  // T must support the + operator
}