Post

the Member Initializer List in C++

Understanding the Member Initializer List in C++

When creating objects in C++, especially those with complex data members, efficiency matters. One way to make object initialization more efficient is through the member initializer list. This feature allows you to initialize class members directly before the constructor body executes, avoiding unnecessary default construction and subsequent assignments.

What is a Member Initializer List?

A member initializer list is a part of the constructor definition that allows you to initialize class data members right after the constructor signature. It is introduced with a colon (:) followed by a comma-separated list of members and their values. This method is particularly useful for initializing constant members, references, and complex objects like vectors.

Why Use a Member Initializer List?

Using a member initializer list provides several benefits:

  • Efficiency: It avoids unnecessary default construction and later assignment of values, making the process more efficient.
  • Direct Initialization: Members are initialized directly, which is required for certain types like const members or references.
  • Cleaner Code: Initialization code is separate from the constructor body, improving readability.

Example 1: Using a Member Initializer List

Here’s a simple example that demonstrates the use of a member initializer list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>

class Building {
public:
    Building(int numberOfFloors)
        : floors(numberOfFloors, 0) { // Member initializer list
        std::cout << "Creating building with " 
                  << numberOfFloors << " floors." << std::endl;
    }

private:
    std::vector<int> floors; // Vector of integers
};

In this code:

  • The : floors(numberOfFloors, 0) part is the member initializer list. It directly initializes the floors vector with numberOfFloors elements, each initialized to 0.
  • This happens before the constructor’s body executes, ensuring the vector is efficiently created with the desired size and values.

Example 2: Without a Member Initializer List

Now, let’s see what it looks like if we don’t use a member initializer list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

class Building {
public:
    Building(int numberOfFloors) {
        // Initialization in the constructor body
        floors = std::vector<int>(numberOfFloors, 0);

        std::cout << "Creating building with " 
                  << numberOfFloors << " floors." << std::endl;
    }

private:
    std::vector<int> floors; // Vector of integers
};

Here’s what happens in this version:

  • The floors vector is first default-constructed (i.e., it starts as an empty vector).
  • Then, in the constructor body, we assign a new vector with numberOfFloors elements, each set to 0.
  • This two-step process is less efficient, as it involves creating an empty vector first and then assigning a new vector, potentially involving memory reallocation.

When to Use a Member Initializer List

  • When initializing complex data members like std::vector, std::string, or other classes.
  • For const data members and references, which must be initialized at the point of their declaration.
  • When you want to avoid unnecessary default construction and assignments for better performance.

Conclusion

The member initializer list is a powerful tool in C++ that provides an efficient and clean way to initialize class members. By initializing members directly, you save on unnecessary assignments and memory operations, leading to more efficient code. Whenever possible, use a member initializer list to take advantage of direct initialization.

This post is licensed under CC BY 4.0 by the author.

Impressum  | 

Datenschutz  | 

Manage cookie settings  | 

Using the Chirpy theme for Jekyll

© 2024 CodingTarik. Some rights reserved.