15.2 — R-value references

Way back in chapter 1, we mentioned l-values and r-values, and then told you not to worry that much about them. That was fair advice prior to C++11. But understanding move semantics in C++11 requires a re-examination of the topic. So let’s do that now.

L-values and r-values

Despite having the word “value” in their . . . → Read More: 15.2 — R-value references

15.1 — Intro to smart pointers and move semantics

Consider a function in which we dynamically allocate a value:

Although the above code seems fairly straightforward, it’s fairly easy to forget to deallocate ptr. Even if you do remember to delete ptr at the end of the function, there are a myriad of ways that ptr may not be deleted if the function . . . → Read More: 15.1 — Intro to smart pointers and move semantics

14.x — Chapter 14 comprehensive quiz

Chapter review

Exception handling provides a mechanism to decouple handling of errors or other exceptional circumstances from the typical control flow of your code. This allows more freedom to handle errors when and how ever is most useful for a given situation, alleviating many (if not all) of the messiness that return codes cause.

A . . . → Read More: 14.x — Chapter 14 comprehensive quiz

14.7 — Function try blocks

Try and catch blocks work well enough in most cases, but there is one particular case in which they are not sufficient. Consider the following example:

In the above example, derived class B calls base class constructor A, which can throw an exception. Because the creation of object b has been placed inside a . . . → Read More: 14.7 — Function try blocks

14.6 — Rethrowing exceptions

Occasionally you may run into a case where you want to catch an exception, but not want to (or have the ability to) fully handle it at the point where you catch it. This is common when you want to log an error, but pass the issue along to the caller to actually handle.

When . . . → Read More: 14.6 — Rethrowing exceptions

8.15 — Nested types in classes

Consider the following short program:

There’s nothing wrong with this program. But because enum FruitType is meant to be used in conjunction with the Fruit class, it’s a little weird to have it exist independently from the class itself.

Nesting types

Unlike functions, which can’t be nested inside each other, in C++, types can . . . → Read More: 8.15 — Nested types in classes

13.x — Chapter 13 comprehensive quiz

Templates allow us to write functions or classes using placeholder types, so that we can stencil out identical versions of the function or class using different types. A function or class that has been instantiated is called a function or class instance.

All template functions or classes must start with a template parameter declaration that . . . → Read More: 13.x — Chapter 13 comprehensive quiz

13.8 — Partial template specialization for pointers

In previous lesson 13.4 — Template expression parameters, we took a look at a simple templated Storage class:

We showed that this class had problems when template parameter T was of type char* because of the shallow copy/pointer assignment that takes place in the constructor. In that lesson, we used full template specialization to . . . → Read More: 13.8 — Partial template specialization for pointers

13.5 — Function template specialization

When instantiating a function template for a given type, the compiler stencils out a copy of the templated function and replaces the template type parameters with the actual types used in the variable declaration. This means a particular function will have the same implementation details for each instanced type (just using different types). While most . . . → Read More: 13.5 — Function template specialization

12.x — Chapter 12 comprehensive quiz

And so our journey through C++’s inheritance and virtual functions comes to an end. Fret not, dear reader, for there are plenty of other areas of C++ to explore as we move forward.

Chapter summary

C++ allows you to set base class pointers and references to a derived object. This is useful when we want . . . → Read More: 12.x — Chapter 12 comprehensive quiz