A union is a struct in which all members are allocated at the same address so that the union occupies as much space as its largest member, e.g.
// This is a naked union because it doesn't have an associated indicator // for which member it holds. union Value { Node* p; int i; } // The language doesn't keep track of which kind of value is held by a // union, so the programmer must do that themselves, e....
Unscoped (or Plain or C-Style) Enumerations Plain (or C-style) enums are entered in the same scope as the name of their enum, and implicitly convert to their integer value, e.g.
enum Color { red, green, blue }; int col = green; Color c2 = 1; // error: invalid conversion from 'int' to 'Color' [-fpermissive] enum CardColor { red, black }; // Error: "red" conflicts with a previous declaration...
A struct helps us organize the elements that a type needs into a data structure, e.g.
struct Vector { int sz; // number of elements double* elem; // pointer to elements on the free store }; void vector_init(Vector& v, int s) { v.elem = new double[s]; v.sz = s; }
However, notice that a user of Vector has to know every detail of a Vector’s representation. We can improve on this....
Resource Handles RAII is also covered in Classes in C++ > Motivation for the Destructor Mechanism The constructor/destructor pattern enables objects defined in a scope to release the resources during exit from the scope, even when exceptions are thrown. All standard-library containers, e.g. std::vector, are implemented as resource handles.
std::unique_ptr and std::shared_ptr These “smart pointers” are useful in managing objects that are allocated on the free store (as opposed to those allocated on the stack)....
A template is a class or a function that we can parameterize with a set of types or values.
Parameterized Types The vector-of-doubles can be generalized to a vector-of-anything type by making it a template:
// `template<typename T>` can be read as "for all types T". Older code // uses `template<class T>`, which is equivalent. template<typename T> class Vector { public: explicit Vector(int s); ~Vector() { delete[] elem; } // ....
quotes Doug McIlroy:
Those types are not “abstract”; they are as real as int and float.
What is the context of this quote?
A class is a user-defined type provided to represent a concept in the code of a program. Essentially, all language facilities beyond the fundamental types, operators, and statements exist to help define better class or to use them more conveniently....