Main Content

CERT C++: OOP53-CPP

Write constructor member initializers in the canonical order

Since R2020a

Description

Rule Definition

Write constructor member initializers in the canonical order.1

Polyspace Implementation

The rule checker checks for Members not initialized in canonical order.

Examples

expand all

Description

Members not initialized in canonical order occurs when the initializer list of a class constructor:

  • Does not initialize data members of the class in the order in which they are declared.

    For instance:

    class aClass {
       int var1;
       int var2;
    public: 
       aClass(int val): var2(val), var1(val) {}
    };

  • Does not call base class constructors in the order in which they appear in the base-specifier list.

    For instance:

    class aClass: baseClass1, baseClass2 {
       aClass(int val): baseClass2(val), baseClass1(val) {}
    }

Risk

The order in which data members or base class constructors appear in the initializer list does not reflect the actual order of initialization. Data members are initialized in the order of declaration and base class constructors are called in the order in which they appear in the base-specifier list.

However, you or another developer can mistake the order in the initializer list as the actual initialization order. As a result, you might introduce dependencies between the initializations that results in reading an uninitialized region of memory. For instance, this initializer list might indicate that bVar is first initialized with the constructor argument x and then aVar is initialized with bVar:

class aClass {
   int aVar;
   int bVar;
public: 
   aClass(int x): bVar(x), aVar(bVar) {}
};
However, the initialization happens in the order of declaration and an uninitialized bVar is read first.

Fix

In the initializer list of a class constructor:

  • Specify class data members in the same order as you declare them in the class

    For instance:

    class aClass {
       int var1;
       int var2;
    public: 
       aClass(int val): var1(val), var2(val) {}
    };

  • Call base constructors in the same order as you specify them in the base-specifier list.

    For instance:

    class aClass: baseClass1, baseClass2 {
       aClass(int val): baseClass1(val), baseClass2(val) {}
    }

Check Information

Group: 09. Object Oriented Programming (OOP)

Version History

Introduced in R2020a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.