AUTOSAR C++14 Rule A12-1-1
Constructors shall explicitly initialize all virtual base classes, all direct non-virtual base classes and all non-static data members
Description
Rule Definition
Constructors shall explicitly initialize all virtual base classes, all direct non-virtual base classes and all non-static data members.
Rationale
If a derived class does not explicitly initialize the constructors
of its base class, then the compiler implicitly initializes the base
class. An object of invalid state or an object with unintended initial
values might be constructed, which risks unexpected code behavior during
run time. Consider this diamond class hierarchy, where the base class
Parent
has multiple constructors.
class Parent{ public: Parent(){/*...*/} Parent(int i){/*...*/} }; class Child1: public virtual Parent{ public: Child1(): Parent(2){/*...*/} }; class Child2: public virtual Parent{ public: Child2(): Parent(1){/*...*/} }; class GrandChild: public Child1, public Child2{ public: GrandChild(){/*...*/} }
GrandChild
object, it is unclear whether Parent
is constructed
by using 1
or 2
as the argument.
Having GrandChild
explicitly specify the constructor
used to initialize the Parent
resolves the ambiguity.
To avoid invalid state and unintended initial values, directly call the
necessary base class constructors in the derived class constructor
initialization list.Polyspace Implementation
Polyspace® flags the constructor of a derived class if its initialization list:
Does not explicitly call the constructors of the virtual base classes.
Does not explicitly call the constructors of the direct nonvirtual base classes.
Does not explicitly initialize nonstatic data members.
If a nonstatic data member is initialized in at least one branch of a branching, looping, or exception handling statement, Polyspace considers the data member to be initialized.
Polyspace does not report a violation of this rule if you use delegating 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, Automated |