Different initialization techniques

Hi, again, I’m wondering what is the point of using the curly initialization.

Obviously, whenever we want to save some memory space without allocating the default data to the variable. That is fully understandable. But if we have from the beginning an idea of the variable content, shouldn’t that be easier to just use the “=" operator.

int x = 5; == int x{5}; ← is this correct?

int x{} is the same as writing int x = 0, that is correct. Either option helps us prevent having our variables getting loaded with junk data that can cause unexpected behavior in our program (you won’t usually see this in small projects but can become an issue in larger projects that are more sensitive).

This does not save on memory, declaring the variable means memory will be allocated for it regardless if we initialize it ourselves or let junk data be used instead.

Ok, understood thank You for Your reply.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms