AutosarC++18_03-A12.1.1¶
Constructors shall explicitly initialize all virtual base classes, all direct non-virtual base classes and all non-static data members
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
A constructor of a class is supposed to completely initialize its object. Explicit initialization of all virtual base classes, direct non-virtual base classes and non-static data members reduces the risk of an invalid state after successful construction.
Example
// $Id: A12-1-1.cpp 271696 2017-03-23 09:23:09Z piotr.tanski $
#include <cstdint>
class Base {
// Implementation
};
class VirtualBase {};
class A : public virtual VirtualBase, public Base {
public:
A()
: VirtualBase{}, Base{}, i{0}, j{0} // Compliant
{}
A(A const &oth)
: Base{}, j{0} // Non-compliant - VirtualBase base class and member
// i not initialized
{}
private:
std::int32_t i;
std::int32_t j;
static std::int32_t k;
};
std::int32_t A::k{0};
See Also
- MISRA C++ 2008 [6]: Rule 12-1-2 All constructors of a class should explicitly call a constructor for all of its immediate base classes and all virtual base classes.
- HIC++ v4.0 [8]:12.4.2 Ensure that a constructor initializes explicitly all base classes and non-static data members.
- JSF December 2005 [7]: AV Rule 71: Calls to an externally visible operation of an object, other than its constructors, shall not be allowed until the object has been fully initialized.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
default_ctor_missing_fields |
Compiler-generated constructor leaves some fields uninitialized. |
None |
False |
implicit_field_init |
Field is only implicitly initialized in constructor. |
None |
False |
missing_base_class_init |
Base class is not explicitly initialized in constructor. |
None |
False |
missing_field_init |
Field is not initialized in constructor. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
exclude_aggregates¶
exclude_aggregates : bool = False
include_empty_classes¶
include_empty_classes : bool = True
include_pure_virtual¶
include_pure_virtual : bool = True
init_functions¶
init_functions : set[bauhaus.analysis.config.FunctionName] = {'Init', 'init'}
inspect_directly_called_methods¶
inspect_directly_called_methods : bool = False
only_member_initializer_list¶
only_member_initializer_list : bool = False
report_missing_field_constructors¶
report_missing_field_constructors : bool = True