Constructor Rules

Since C++11 a constructor can delegate its work to another constructor of the same class. This is the modern way in C++ to put common actions for all constructors in one constructor. In C++ code before C++11, you often used for such a job an init function.

There is a danger using inheriting constructors. If your derived class such as Rec2 has own members, they are uninitialised (1).

Copy and Move

The chapter starts with a meta-rule. Values types, also known as types that behave like an int, should be copyable, but interfaces in class hierarchies not. The last rule C.67 refers to this meta-rule.

C.61 and C.64 say that a copy (move) operation should actually copy (move). This is the expected semantic for a = b.

In case of copying, this means that after copying a and b (a = b) a must be the same: (a ==b).

Copying can be a deep or shallow. Deep copying means that both objects a and b are afterwards totally independent of each other (value semantic). Shallow copying means that both objects a and b share an object afterwards (reference semantic).

C.64 states the moved-from object should be in a valid state. Most of the times this is the default state of the source. The C++ standard requires, that the moved-from object must be afterwards in an unspecified but valid state.

C.62 and C.65 state the same. Copy (move) assignment should be safe for self-assignment. x = x should not change the value of x.

Copy (move) assignment of the containers of the STL, std::string and built- type such as int is safe for self-assignment; therefore, the default generated copy (move) assignment operator is in this case safe for self-assignment. The same will hold for an automatic generated copy (move) assignment operator which uses types that are safe for self-assignment.

The code snippet shows that there is no test for self-assignment such as in the next example necessary. Here is the version of the type Foo with redundant (expensive) checks (1) and (2) for self-assignment.

In this scenario, the copy operations of the base class are used; therefore, only the base part of d is copied.

From the object-oriented perspective an instance of Derivedis-a instance of Base. That means, whenever you need an instance of Base you can use an instance of Derived. But you have to be careful. If you take the instance of Base by copy (value-semantic), you will only get the base parts of an instance of Derived.

What's next?

Get your e-book at leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11 and C++14 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 500 pages full of modern C++ and more than 100 source files presenting concurrency in practice.