Category cpp_nibble

#104 – The elegant way to initialize structs

A simple struct is often just a small bundle of related data. When you create one, you usually want its members to start with meaningful values immediately. Aggregate initialization lets you do that in one statement, instead of creating the object first…

#103 – Printing and parsing enums with << and >>

Enumerations are great however printing them out are quite tricky. For unscoped enums, they implicitly convert to an int. For scoped enums, the type safety is enforced. Fortunately, there is a solution! This Nibble will demonstrate how to use operator…

#102 – Two simple, practical uses for enums in C++

Enumerations are used to store a bundle of information. They have numerous applications. This Nibble will demonstrate 2 such applications. 1. Returning status codes Suppose a function validates a password. A weak version might return integers: This technically works, but…

#100 – Why you should avoid using out parameters

A function and its caller communicate with each other via two mechanisms: parameters and return values. When a function is called, the caller provides arguments, which the function receives via its parameters. Parameters normally send information into a function. Return…

#097 – 2 ways to manipulate pointers

A pointer points to a memory address. These gives you two different levers to manipulate pointers: Three things to take away: Operation 1: Change the address of the pointer Assigning a new address to a pointer changes what object it…

#096 – Why const T& is more flexible than T

An lvalue reference can only bind to a modifiable object. Therefore, the code excerpt below throws a compiler error. The workaround is to simply make the lvalue reference const. Lvalue reference to const By labelling the lvalue reference as const…