C++
Notes
In order to translate a C++ program (as a plain text file) into an executable program, two main operations need to happen:
compilation: C++ source files are converted into an intermediate format called object files;
linking: find where each symbol is and link them together - reconstruct a single executable application from many individual object files.
Pre-processor directives include:
#include
: when the pre-processor finds this directive, it will copy and paste the contents of the file being included;#define
: when the pre-processor finds this directive, it will replace the macro with its definition (a macro can also have arguments);#if
...#end
: the processor will or will not include the portion of code between these two directives depending on whether the condition of the directive is true (1) or false (0)
Header files are useful to share definitions among different files, so that it is possible to use in each compilation unit (=C++ file) variables, classes, and functions defined somewhere else.
To avoid including multiple times the same header files (and hence definitions) in a compilation unit, there are two main techniques:
the
#pragma once
directive (non-standard but almost universally supported)an include guard - usually implemented via a
#ifndef
...#endif
block:
Includes with the double quotes (e.g.,
#include "Log.h"
) are used for header files in the same directory as the source file, while includes with angular brackets (e.g.,#include <iostream>
) are used for standard library headers.A pointer is an integer variable containing a memory address. It is possible to retrieve the address of a variable with the ampersand operator (e.g.,
int* ptr = &myvar
) and it's possible to get back the value contained at the address stored in the pointer with the asterisk operator (e.g.,int mynewvar = *ptr
).A reference is syntax sugar on top of pointers to make it a bit easier to work with: it is not a variable with storage requirements, but simply a reference to an existing variable. It is denoted with an ampersand next to the type (e.g.,
int& reference = a
wherea
is a variable of typeint
). It is useful especially in function declarations to pass arguments by reference (i.e., with a pointer) rather than by value (i.e., creating a copy of the value to be used inside the function).Classes allow to define types containing both data and functions (called methods) that act upon them. The only difference between a class and a struct is that, by default, all members of a class are private, while all members of a struct are public.
The static variable has two different meanings, depending on the context where it's used:
inside classes, a static variable/method will be shared among all instances of the class where the static variable is defined (NB a static method can only access static variables in its body);
outside classes, static means the linkage of the symbol declared as static will be limited to the translation unit (=C++ file) where it was defined.
The constructor is a special method of a class that gets automatically called when a new object is created, and is used to properly initialise all member variables of an object. The destructor, instead, is the special method that will be called when an object is about to be destroyed (either by exiting its scope or when the operator
delete
is called).
Resources
Books
Modern C++ Tutorial: C++ 11/14/17/20 On the Fly - Changkun Ou (GitHub)
Frameworks
U++ - Cross-Platform App Development Framework
GitHub repositories
Reddit Threads
Videos
C++ Dynamic Linking vs Static Linking - Jamie King
Websites
C++ Developer Guide - abseil
C++ Tips of the Week - abseil
YouTube playlists
Advanced C++ - Bo Qian
C++ - The Cherno
C++ STD Gems - ChiliTomatoNoodle
C++ Unit Test - Bo Qian
Modern C++ - Bo Qian
Last updated