AUTOSAR C++14 Rule A12-1-5
Common class initialization for non-constant members shall be done by a delegating constructor
Since R2021a
Description
Rule Definition
Common class initialization for non-constant members shall be done by a delegating constructor.
Rationale
C++ classes often have several constructors with different syntaxes. These initializers
might have some initializations in common. For instance, in this code, both constructors of
the class Circle
initialize the nonconstant members x
,
y
and
r
.
class Circle{ int x; int y; int r; public: Circle(int x_in, int y_in, int r_in): x{x_in}, y{y_in},r{r_in}{ //... } Circle( int x_in, int y_in): x{x_in}, y{y_in}, r{0}{ //... } //... };
To avoid unexpected results, delegate the initialization of nonconstant members to existing constructors whenever possible. Avoid repeating initializations in multiple constructors.
Polyspace Implementation
Polyspace® flags a class, union, or structure if any of their nonconstant members are initialized in multiple constructors. Polyspace does not flag:
Copy or move constructors that do not use delegate constructors.
Arrays that are initialized in multiple constructors.
Objects that are initialized field by field in multiple constructors.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Special member functions |
Category: Required, Partially automated |
Version History
Introduced in R2021a