Rule A12-1-5 (required, implementation, partially automated)

Common class initialization for non-constant members shall be done by a delegating constructor.

Rationale

Common initialization of non-constant members in a delegating constructor prevents from code repetition, accidental differences and maintenance problems.

Example

// $Id: A12-1-5.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $

#include <cstdint>

class A
{
public:
// Compliant
A(std::int32_t x, std::int32_t y) : x(x + 8), y(y) {}
explicit A(std::int32_t x) : A(x, 0) {}

private:
std::int32_t x;
std::int32_t y;

};

class B
{
public:
// Non-compliant
B(std::int32_t x, std::int32_t y) : x(x + 8), y(y) {}
explicit B(std::int32_t x) : x(x + 8), y(0) {}

private:
std::int32_t x;
std::int32_t y;

};

See also

HIC++ v4.0 [9]: 12.4.5: Use delegating constructors to reduce code duplication. C++ Core Guidelines [11]: C.51: Use delegating constructors to represent common actions for all constructors of a class.