Skip to content

Latest commit

 

History

History
49 lines (34 loc) · 2.5 KB

File metadata and controls

49 lines (34 loc) · 2.5 KB

Constructor Initialization Lists in C++

In C++, a constructor initialization list is used to initialize member variables before the constructor's body is executed. This is not just a matter of style, but also of performance and correctness, especially when dealing with constants, reference members, or objects of classes that don't have default constructors.

Syntax and Usage

Here is the basic syntax of an initialization list:

ClassName(Type1 arg1, Type2 arg2, ...) : member1(arg1), member2(arg2), ... {
    // Constructor body
}
  • The initialization list begins with a colon (:) after the constructor parameters.
  • Each member variable is followed by parentheses containing arguments for its initialization.
  • The initializations occur in the order the members are declared in the class, not the order listed in the initialization list.

Advantages of Initialization Lists

  • Efficiency: For non-static data members that are objects of other classes, if we do not use an initializer list, then the default constructor for those objects is called first, and then the assignment operation is used to assign the value. With the initializer list, the copy constructor is called to directly initialize the object, which is often more efficient.
  • Initialization of consts and references: Constants and reference variables cannot be assigned values; they must be initialized. Initialization lists provide a way to initialize these types of members.
  • Initialization of members of a base class and non-default constructible members: When deriving from a class or when a class has members that can only be initialized with specific values (like an object of a class with no default constructor), initializer lists must be used.

Example

class Point {
    const int x; // Constant member
    const int y; // Constant member

public:
    // Constructor with initialization list
    Point(int xPos, int yPos) : x(xPos), y(yPos) {
        // Constructor body (empty in this case)
    }

    void print() const {
        std::cout << "(" << x << ", " << y << ")" << std::endl;
    }
};

In the example above, x and y are constant members of Point and must be initialized using an initialization list.

Conclusion

Constructor initialization lists are a powerful feature in C++ that allows for efficient and correct initialization of class members. They are particularly useful when working with constants, reference members, objects that require non-default initialization, and when optimizing for performance.