07-03-2017, 08:22 AM
The main statement of rule 8-5-1 is:
But what about non-static members of a class being of type class.
Must a constructor of the class member variable explicitly called in the constructor to be compliant to this rule?
Example:
The default constructor of class A for member variable m_b is in both constructors of class B called, but in default constructor of class B only implicit according to C++ standard.
Is the default constructor of class B not explicitly calling default constructor of class A for member variable m_b compliant to rule 8-5-1?
MISRA C++:2008 Wrote:Each class constructor shall initialize all non-static members of its class.The example shows a compliant and a non compliant constructor of a class having only member variables of basic numerical type int32_t. It is clear that a member variable of a basic type must be initialized before first read access to avoid an undefined behavior.
But what about non-static members of a class being of type class.
Must a constructor of the class member variable explicitly called in the constructor to be compliant to this rule?
Example:
Code:
class A
{
public:
A ( ) : m_a( false )
{
}
bool GetmA ( void )
{
return ( m_a );
}
private:
bool m_a;
};
class B
{
public:
B ( ) : m_a( false ) // Compliant or non-compliant because of
{ // not explicitly calling constructor of m_b?
}
B ( const bool a ) : m_a( a ), m_b() // Definitely compliant
{
}
bool GetmA ( void )
{
return ( m_a );
}
bool GetmB (void )
{
return m_b.GetmA();
}
private:
bool m_a;
A m_b;
};
int main ( void )
{
B b;
if ( b.GetmB() )
{
}
}
Is the default constructor of class B not explicitly calling default constructor of class A for member variable m_b compliant to rule 8-5-1?