D
D
D_bl_M2013-12-06 07:53:35
C++ / C#
D_bl_M, 2013-12-06 07:53:35

How to initialize C++11 variables?

How do you initialize variables?

int emus {7};         // устанавливает emus в 7 
int rheas = {12};    // устанавливает rheas в 12 
int rocs = {};        // устанавливает rocs в 0 
int psychics {};     // устанавливает psychics в 0

Do you omit the equals sign or not, and why?
Please express your opinion and ideas in the comments.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rio, 2013-12-06
@Rio

int n = 42;
Because it is more familiar, clearer and shorter.

A
Andrew Dabich, 2013-12-06
@dabich

You can also do this:
int n(10);
Sometimes I omit, because it is short and just as clear.

D
D_bl_M, 2013-12-06
@D_bl_M

The fact is that curly braces are from the new standard

int n = 42;
Because it is more familiar, clearer and shorter.

It seems to me shorter so:
int n {}; // defined by zero
rather than
int n = 0;
That and actually your version is outdated, nothing personal, I understand that it's a matter of habit, I'm just collecting opinions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question