Chapter 4 – Classes: Part 1

Compiler-generated functions

In C++03, we had 4 functions that were generated automatically by the compiler (constructor, destructor, copy constructor, and copy assignment).

In C++11, due to a new feature (move semantics), we have 2 more compiler-generatted functions – move constructor and move assignment.

Good practice says that if we see the need to modify one of these functions (except the constructor), we should probably modify all of them. That means that we probably do some specific memory allocation, which should be performed on both creation (through copy, or assignment) and on destruction (via destructor).

Keywords: default, delete

Default is a keyword that works only on the compiler-generated functions (See above) whereas delete works on all functions. (Could be used to disable copy or move operations, for example, but also to disallow some implicit casting being performed).

In the code above, we delete the copy construction and copy assignment, so the last two lines will not compile.

In-class member initialization

This feature means that we can use initialization directly where we create the member variables.

Delegating constructors

This is a simple feature, it means we can call a constructor from another constructor. This cannot be recursive, as it needs to stop, and no other data can be initialized in the delegating constructor.

Inheriting constructors

This feature allows us to inherit constructors from the base class, and pass further to it the data received by our derived class, to avoid code duplication. We can also use it in addition to other constructors we have.

You may also like...