Random Link ¯\_(ツ)_/¯ | ||
Jun 4, 2022 | » | Unions in C++
2 min; updated Jun 4, 2022
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.... |
Jun 4, 2022 | » | Enumerations in C++
7 min; updated Jun 4, 2022
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... |
Jun 4, 2022 | » | Structures in C++
1 min; updated Jun 4, 2022
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.... |
Jun 4, 2022 | » | User-Defined Types
(5 items)
Classes in C++; Structures in C++; Enumerations in C++; Unions in C++; Inheritance; |
May 31, 2022 | » | Resource Management in C++
4 min; updated May 31, 2022
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).... |
May 31, 2022 | » | Resource Management
(1 items)
Resource Management in C++; |
May 30, 2022 | » | Templates in C++
4 min; updated May 30, 2022
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; } // .... |
May 12, 2022 | » | Classes in C++
15 min; updated May 12, 2022
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.... |