Show Menu
Cheatography

A cheatsheet for C++

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

The -> operator

Dog* d = new Dog("Rex");

d->bark();    // shorthand
(*d).bark();  // same thing
-> operator — shorthand for derefe­rencing a pointer and accessing a member. Equivalent to (*poin­ter­).m­ember.

Building a class with a header file

// Foo.h
struct Foo {
    <Type> <attributeName>;

    static <ReturnType> <functionName>(<Type> <variable>);
    static <ReturnType> <functionName>(<Type> <variable>);
    static <ReturnType> <functionName>(<Type> <variable>);
};

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

<ReturnType> Foo::<functionName>(<Type> <variable>)
{
    // implementation
}

<ReturnType> Foo::<functionName>(<Type> <variable>)
{
    // implementation
}

<ReturnType> Foo::<functionName>(<Type> <variable>)
{
    // implementation
}

enum class

enum class <EnumName> { <Val1>, <Val2>, <Val3> };

<EnumName> <var> = <EnumName>::<Val_n>; // where n is an Val in the definition.
You can think of these as a variable, but ahead of time it is constr­ained to a specific set of values.

Definition of a Dangling Pointer

int* createNumber() {
    int x = 42;   // lives on the stack
    return &x;    // return its address
}                 // x is destroyed here

int main() {
    int* p = createNumber();
    *p;  // ❌ dangling — x is gone, memory no longer belongs to you
}
Dangling pointer — a pointer that points to memory that no longer belongs to you. The object it pointed to has been destroyed, but the pointer still holds its old address.

Address of Operator

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

Definition of a Caller

int add(int a, int b) {  // callee
    return a + b;
}

int main() {             // caller
    add(1, 2);
}
Caller — the function that calls another function.

Passing Functions (Callb­acks)

#include <iostream>

void sayHello()
{
    std::cout << "Hello!" << std::endl;
}

void execute(void (*callback)())
{
    callback();
}

int main()
{
    execute(&sayHello);  // pass the function, not call it
}
In C++, functions can be passed as parameters to other functions using function pointers, allowing you to decide what code runs without hardcoding it. The receiving function holds onto the address of the passed function and calls it at the right time. This is the foundation of event-­driven progra­mming, where you say "call this function when something happen­s"
 

Defnition of a Thread

#include <thread>
#include <iostream>

void doWork() {
    std::cout << "thread running\n";
}

int main() {
    std::thread t(doWork);  // thread starts here
    t.join();               // main waits for it to finish
}
An indepe­ndent sequence of execution within a program. The OS can run multiple threads concur­rently, each doing their own work, while sharing the same memory.

L values (Left Values)

int x = 10;        // x is an lvalue
x = 20;            // valid: lvalue on the left of =

int y = x + 1;     // (x + 1) is an rvalue — temporary, no fixed address
int z = 42;        // 42 is an rvalue literal
// &(x + 1);       // ERROR: cannot take address of an rvalue
An lvalue (locator value) is an expression that refers to a memory location and can appear on the left-hand side of an assign­ment. It represents an object that persists beyond a single expression — it has an identi­fiable address in memory.

Definition of a Static Method

class MathHelper {
public:
    static int add(int a, int b) {
        return a + b;
    }
};

int result = MathHelper::add(3, 5);  // 8
A static method is a function that belongs to the class itself rather than to any instance of it. It has no access to instance attributes and can be called without ever creating an object.

Definition of a Signature

#include <iostream>

void greet(std::string name) {    // signature: greet(std::string)
    std::cout << "Hello, " << name << "!\n";
}

int main() {
    greet("Alice");  // → Hello, Alice!
}
A function signature in C++ is the part of a function declar­ation that identifies it uniquely to the compiler. It consists of:

Function name
Parameter types (number, order, and types)

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

bitwise OR assignment operator

|=

// Example

bool result = false;  // 0
result |= false;      // 0 | 0 = 0
result |= false;      // 0 | 0 = 0
result |= true;       // 0 | 1 = 1
result |= false;      // 1 | 0 = 1  — stays true
result |= false;      // 1 | 0 = 1  — stays true
// result is true
Claude responded: Bitwise OR assignment (|=) — takes the existing bits of the left operand, ORs them column by column with the bits of the right operand, and stores the result b…Bitwise OR assignment (|=) — takes the existing bits of the left operand, ORs them column by column with the bits of the right operand, and stores the result back into the left operand. Can only turn bits on, never off.
 

Lambda Functions

[&<OBJECT>] {
    <CODE>;
    <CODE>;
}
The brackets tell the lambda which variables from the surrou­nding scope it's allowed to use, and how it remembers them.

By default, a lambda body cannot see any local variable from the function it's written inside. The capture list is how you grant access, one variable (or one default rule) at a time.

Constr­ucting an obect in C++

TypeName variableName(constructorArguments);
This is how it would look in Python:
variab­le_name = ClassN­ame­(co­nst­ruc­tor­_ar­gum­ents)

Derefe­rencing a Pointer

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

*p;           // dereference — follow the pointer to get 42
The * in front of a pointer means "go to the address this pointer holds and give me what's there."­

Range Based For-Loops

for (const <type>& <element> : <collection>) {
    // use <element>
}
A range-­based for loop is a loop that iterates over every element in a collec­tion, from the first to the last, without needing to manually manage an index or i…A range-­based for loop is a loop that iterates over every element in a collec­tion, from the first to the last, without needing to manually manage an index or iterator.

const — omit if you need to modify <el­eme­nt>
& — omit if you want a copy of each element rather than a reference

Template Functions

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

Try Catch

try {
    // code that might throw an exception
} catch (const std::exception& e) {
    // handle exception
}

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;