Henrik Samuelsson’s Reviews > C++ Crash Course: A Fast-Paced Introduction > Status Update
Henrik Samuelsson
is on page 153 of 792
Static members are members of a class that aren’t associated with a particular instance of the class.
Example:
struct RatThing {
static int rat_things_power; // A static member.
};
int RatThing::rat_things_power = 200; // Initialize the static member
int main() {
// snip
}
— Aug 31, 2023 10:44PM
Example:
struct RatThing {
static int rat_things_power; // A static member.
};
int RatThing::rat_things_power = 200; // Initialize the static member
int main() {
// snip
}
Like flag
Henrik’s Previous Updates
Henrik Samuelsson
is on page 180 of 792
We can get an copy of objects from:
1. Copy constructor i.e. Object(const Object& otherObject)
2. Copy assignment i.e. the = operator
There will be a default versions of the above but they might not behave as desired since there will be no deep copying. we will then instead define our own versions.
— Sep 07, 2023 10:32PM
1. Copy constructor i.e. Object(const Object& otherObject)
2. Copy assignment i.e. the = operator
There will be a default versions of the above but they might not behave as desired since there will be no deep copying. we will then instead define our own versions.
Henrik Samuelsson
is on page 173 of 792
Structured binding is a C++ feature that allows you to bind multiple variables to the elements of a structured object, such as a struct, in a single declaration. This can be for example be used to return multiple different values from a function.
// Inside function called create_car
return {car, production_year}
// Catch the two return values
auto [my_car, year] = create_car();
— Sep 06, 2023 10:10PM
// Inside function called create_car
return {car, production_year}
// Catch the two return values
auto [my_car, year] = create_car();
Henrik Samuelsson
is on page 169 of 792
Allocation and deallocation of memory should be handled within the constructor and destructor of a class to prevent leaking of memory used for storage.
This pattern is called resource acquisition is initialization (RAII) or constructor acquires, destructor releases (CADRe). Where the second name is in my opinion better because it describes the intent.
— Sep 05, 2023 11:44PM
This pattern is called resource acquisition is initialization (RAII) or constructor acquires, destructor releases (CADRe). Where the second name is in my opinion better because it describes the intent.
Henrik Samuelsson
is on page 165 of 792
If the code misbehaves for whatever reason so can this be handled by the use of the exception system that comes with C++.
Say for example that we receive unexpected input that our code can not handle. We can then move to special sections of our code base to try to handle this.
The C++ keywords used for exception implementation is try and catch, first we try to do something and if it fails we catch the exception.
— Sep 04, 2023 10:30PM
Say for example that we receive unexpected input that our code can not handle. We can then move to special sections of our code base to try to handle this.
The C++ keywords used for exception implementation is try and catch, first we try to do something and if it fails we catch the exception.
Henrik Samuelsson
is on page 156 of 792
The keywords new and delete can be used to dynamically allocate memory for objects in C++.
Delete must be called sometimes after new to release the memory. Otherwise there will be a memory leak meaning that more and more memory will be used until we possibly run out of memory.
Think of this as we borrow memory and it is our responsibility to return it.
— Sep 03, 2023 10:19PM
Delete must be called sometimes after new to release the memory. Otherwise there will be a memory leak meaning that more and more memory will be used until we possibly run out of memory.
Think of this as we borrow memory and it is our responsibility to return it.
Henrik Samuelsson
is on page 138 of 792
C++ references can not be reseated. You can reassign a reference but it will not move the reference. What happens is instead that the value at the address that is referenced by the reference will be updated.
In other words pointers and references behaves different when employing the assignment operator =. Reassigning a pointer will set it to another address.
— Aug 28, 2023 10:51PM
In other words pointers and references behaves different when employing the assignment operator =. Reassigning a pointer will set it to another address.
Henrik Samuelsson
is on page 125 of 792
There are unfortunately multiple ways to initialize objects in C++. If unsure what way to initialize an object go for braced initializers:
char name[] = {"Bjarne"};
int year_of_birth{1950};
— Aug 26, 2023 09:40AM
char name[] = {"Bjarne"};
int year_of_birth{1950};
Henrik Samuelsson
is on page 121 of 792
Most Vexing Parse
The most vexing parse is a ambiguity resolution in the C++ programming language. In certain situations, the C++ grammar cannot distinguish between the creation of an object parameter and specification of a function's type. In those situations, the compiler is required to interpret the line as a function type specification. (Wikipedia)
Use braced initialization syntax to avoid most vexing parse.
— Aug 25, 2023 11:07PM
The most vexing parse is a ambiguity resolution in the C++ programming language. In certain situations, the C++ grammar cannot distinguish between the creation of an object parameter and specification of a function's type. In those situations, the compiler is required to interpret the line as a function type specification. (Wikipedia)
Use braced initialization syntax to avoid most vexing parse.
Henrik Samuelsson
is on page 112 of 792
There are two types of enums in C++, scoped and unscoped.
Scoped enums are safer, need to use the type followed by :: when using the values. We use two keywords; enum class to create these enums in the code.
Unscoped enums are supported for legacy reasons, these are created by just using the keyword enum.
— Aug 25, 2023 05:56AM
Scoped enums are safer, need to use the type followed by :: when using the values. We use two keywords; enum class to create these enums in the code.
Unscoped enums are supported for legacy reasons, these are created by just using the keyword enum.

