7

Starting out in c++ and noticed that you could initialise a variable in two ways

int example_var = 3;  // with the assignment operator '='

or

int example_var(3);  // enclosing the value with parentheses 

is there a reason to use one over the other?

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
RandomPhobia
  • 4,698
  • 7
  • 25
  • 22
  • Just so you know, if you test it out on your own type, `MyClass obj = 5;` calls the constructor, and never the assignment operator. I also know this has been asked before; I'll try to find where it went. – chris Aug 27 '12 at 14:22
  • 1
    Technically, neither of these are assignments. They are *initializations*. They define and initialize a new variable. Assignment occurs when you assign a new value to an existing variable. (`int i = 1` is initialization, but `i = 2` is assignment) – jalf Aug 27 '12 at 14:22

4 Answers4

7

The first form dates back from the C time, while the second was added in C++. The reason for the addition is that in some contexts (in particular initializer lists in constructors) the first form is not allowed.

The two are not exactly equivalent for all types, and that is where one or the other might be more useful. The first form semantically implies the creation of a temporary from the right hand side, followed by the copy construction of the variable from that temporary. The second form, is direct initialization of the variable from the argument.

When does it matter?

The first form will fail if there is no implicit conversion from the right hand side to the type of the variable, or if the copy constructor is not available, so in those cases you will have to use direct initialization.

The second form can be used in more contexts than the first, but it is prone to the most-vexing-parse. That is, in some cases the syntax will become compatible with a declaration for a function (rather than the definition of a regular variable), and the language determines that when this is the case, the expression is to be parsed as a function declaration:

std::string s = std::string();  // ok declares a variable
std::string s( std::string() ); // declares a function: std::string s( std::string(*)() )

Finally in C++11 there is a third form, that uses curly braces:

std::string s{std::string{}};

This form has the advantages of direct initialization with parenthesis, but at the same time it is not prone to misinterpretation.

Which one to use?

I would recommend the third option if available. That being said, I tend to use the first more often than not, or the second depending on the context and the types...

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
6

For built in types like int both mean the same.
But for custom data types they can mean different. First format is called Copy Initialization while second is called Direct Initialization.

Good Read:

Is there a difference in C++ between copy initialization and direct initialization?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

Their output is the same... the both the syntax call the copy constructor. It is same for int and other similar built in data types, though some difference is there for user defined data types.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
0

They compile to the same thing. However, both are a form of variable initialization, not assignment, which matters a little in C and a lot in C++ since totally different functions (constructor v. assignment) are called.

djechlin
  • 59,258
  • 35
  • 162
  • 290