What is the copy-and-swap idiom?

Overview Why do we need the copy-and-swap idiom? Any class that manages a resource (a wrapper, like a smart pointer) needs to implement The Big Three. While the goals and implementation of the copy-constructor and destructor are straightforward, the copy-assignment operator is arguably the most nuanced and difficult. How should it be done? What pitfalls … Read more

What is The Rule of Three?

Introduction C++ treats variables of user-defined types with value semantics. This means that objects are implicitly copied in various contexts, and we should understand what “copying an object” actually means. Let us consider a simple example: class person { std::string name; int age; public: person(const std::string& name, int age) : name(name), age(age) { } }; … Read more

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating static_cast isn’t necessary, but it’s important to note that the T(something) syntax … Read more

The Definitive C++ Book Guide and List

Beginner Introductory, no previous programming experience Book Author(s) Description review C++ Primer* * Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review. Stanley Lippman, Josée Lajoie, and Barbara E. Moo (updated for C++11) Coming at 1k pages, this is a very thorough introduction into C++ that covers just … Read more