Main Content

AUTOSAR C++14 Rule A12-6-1

All class data members that are initialized by the constructor shall be initialized using member initializers

Description

Rule Definition

All class data members that are initialized by the constructor shall be initialized using member initializers.

Rationale

It is inefficient to initialize data members of a class by assigning a copy of passed values to them in the body of a constructor. For instance, this code is inefficient:

class foo{
	
private:
	int i;
public:
	foo(int input){
		i = input;
		//...
	}
};
It is more efficient to initialize data members of classes by using member initializers. For instance:

  • Initialize data members by using an initializer list.

  • Initialize data members by using default member initializers.

To increase the efficiency of your code and to protect your code from using an uninitialized data member, use the preceding methods to initialize data members of a class.

Polyspace Implementation

Polyspace® flags the constructor definition of a class if the constructor initializes the nonstatic data members of the class in its body by copying the passed values to the data members. Polyspace does not flag constructors with uninitialized static data members.

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

expand all

This example shows efficient initialization methods of class data members that are compliant with this rule.

#include <cstdint>
#include <string>
using namespace std;
class A
{
public:
	A(int32_t n, string s) : number{n}, str{s} 
	{ //Compliant
		n += 1; // This does not violate the rule
		str.erase(str.begin(),
		str.begin() + 1); // This does not violate the rule
		// Implementation
	}  

private:
	int32_t number;
	string str;
};

class C
{
public:
	C(int32_t n, string s) 
	{ //Compliant
		n += 1; // This does not violate the rule
		str.erase(str.begin(),
		str.begin() + 1); // This does not violate the rule
	}
	// Implementation

private:

	int32_t number = 0;
	string str = "string";
	static double pi;
};
  • The constructor of class A initializes the data members by using an initializer list. This constructor is compliant with this rule.

  • The constructor of class C initializes the data members by using default initialization. These data members cannot be used before they are initialized. This constructor is compliant with this rule. Polyspace does not flag constructors that do not initialize static data members.

This example shows inefficient initialization of class data members that is not compliant with this rule.

#include <cstdint>
#include <string>
using namespace std;
class B
{
public:
	B(int32_t n, string s) 
	{ //Noncompliant
		number = n;
		str = s;
	}
	// Implementation

private:
	int32_t number;
	string str;
};
class E{
public:
	E():E(1,"string")
	{
		
	}
	E(int32_t a, string str) : number(a)
	{//Noncompliant
		
	}
private:
	int32_t number;
	string str;
};

  • The constructor of class B initializes the data members by copying the passed parameters. This initialization is inefficient. The data members of class B might be used before they are initialized. Polyspace flags this inefficient and risky constructor.

  • The default constructor of class E attempts to initialize the nonstatic data members by delegating the initialization to another constructor. The second constructor does not initialize the nonstatic data members by using member initializers. Polyspace flags the second constructor.

Check Information

Group: Special Member Functions
Category: Required, Automated

Version History

Introduced in R2019a