Initializing multiple variables with uniform initialization in C++

In C++, you can use uniform initialization to initialize multiple variables in a single line of code. This approach helps improve readability and reduces the chances of forgetting to initialize a variable.

To initialize multiple variables with uniform initialization, you can use curly braces {} and separate each variable assignment with a comma. Here’s an example:

int x{ 10 }, y{ 20 }, z{ 30 };

In the above code snippet, three variables x, y, and z are initialized with values 10, 20, and 30 respectively.

Uniform initialization works not only for built-in types but also for user-defined types such as classes and structs. Here’s an example:

class MyClass {
public:
    int value;
    std::string text;
};

MyClass obj{ 42, "Hello World" };

In this example, the MyClass object obj is initialized with value set to 42 and text set to “Hello World”.

Uniform initialization is not only limited to initializing variables but can also be used when passing arguments to functions or constructors.

void myFunction(int a, int b, int c) {
    // function logic
}

myFunction({ 1, 2, 3 }); // Using uniform initialization to pass arguments

In this case, the myFunction is called with three integers passed using uniform initialization.

Uniform initialization provides a concise and consistent syntax to initialize multiple variables or pass arguments. It is recommended to use uniform initialization whenever possible, as it promotes cleaner and more readable code.

References:

#C++ #Initialization