Initializing C++ objects with constructors, parentheses, and braces in a C++ example.

Can C++ Initialize an Object in Different Ways?

C++ can initialize an object with no explicit initializer, parentheses, or braces. The form matters: it affects constructor selection, initial values, and whether narrowing conversions are allowed. The examples below use C++17 and distinguish a class object from a scalar variable.

C++ object initialization in a complete example

Save this program as initialization.cpp:

#include <iostream>
#include <vector>

class Counter {
public:
    Counter() : value_(10) {}
    explicit Counter(int value) : value_(value) {}
    int value() const { return value_; }
private:
    int value_;
};

int main() {
    Counter a;       // default initialization
    Counter b{};     // list initialization with empty braces
    Counter c(20);   // direct initialization
    Counter d{30};   // direct-list initialization
    int zero{};      // value-initialized scalar

    std::vector<int> sizes(3, 7);
    std::vector<int> values{3, 7};
    std::cout << a.value() << ' ' << b.value() << ' '
              << c.value() << ' ' << d.value() << ' '
              << zero << '\n';
    std::cout << sizes.size() << ' ' << values.size() << '\n';
}

Compile with g++ -std=c++17 -Wall -Wextra initialization.cpp -o initialization and run the executable. It prints 10 10 20 30 0 on the first line and 3 2 on the second. In this class, the default constructor explicitly initializes the member to 10. The one-argument constructor initializes it to the supplied value.

C++ object initialization is governed by the type and the syntax together. The braces in Counter b{} lead to value-initialization here and invoke the default constructor; the class’s user-provided constructor supplies the member value. Counter d{30} uses list initialization and selects the integer constructor. The keyword explicit still permits the direct forms shown.

Default and value initialization

For a local scalar, int number; is default-initialized and has an indeterminate value. Do not read it before assigning a value. int number{}; instead initializes it to zero. Static-storage variables have different initialization rules, so do not generalize the local example to every declaration.

For a class, default initialization calls an applicable default constructor. That alone does not promise every member is initialized: the constructor and any default member initializers determine their state. A constructor that leaves an integer member uninitialized can leave it indeterminate for a local object. When initializing a C++ object, establish the invariant in the constructor or member initializers.

An empty pair of parentheses in a declaration is a common trap: Counter item(); declares a function returning Counter; it does not create an object. Use Counter item; or Counter item{}; for the intended object, after considering their initialization semantics.

Can C++ initialize an object without a default constructor? Yes: supply arguments matching an accessible constructor. If a type has only a constructor requiring an argument, a declaration with no initializer is ill-formed. This can be a useful design choice when an object needs a value before it can satisfy its invariant. Check the constructor’s contract and handle any exception it can throw.

Parentheses, braces, and overloads

With Counter c(20), direct initialization uses the constructor accepting an integer. Braces also select a constructor, but list initialization rejects narrowing conversions such as int x{3.5};. This diagnostic can catch an accidental loss of information. Parenthesized initialization permits more implicit conversions, subject to the language’s normal rules.

Braces can also favor an initializer_list constructor. In the program, std::vector<int> sizes(3, 7) creates three elements with value 7, while values{3, 7} creates two elements containing 3 and 7. When C++ initializes objects with braces, inspect the available constructors rather than assuming braces are interchangeable with parentheses.

The syntax rules evolve with the language standard. In particular, C++20 added parenthesized aggregate initialization in cases where C++17 did not support it. Compile examples in the project’s selected standard mode and test the actual type and overloads before changing initialization style across a codebase.