Is one variable initialization better than the other?

I see that there are 2 different ways to initialize variables in C++:

1
2
int owls = 101;	// traditional C initialization
int wrens(432);  // alternative C++ syntax, set wrens to 432 


Is one way better than the other, or is it just preference?
There's no difference with basic types.
With classes there may be difference if they have explicit constructors
closed account (1yR4jE8b)
No difference with basic types.

With user defined types, a temporary object then copy construction for the first *may* happen instead of in-place construction. Some compilers are smart enough to not do this, although it's not guaranteed by the standard to happen. Also, if there are non-explicit constructors, then automatic type conversion may occur.

The second guarantees that an object is always constructed instead of copy constructed, so technically, more efficient.

A lot of times, they are the same.
With user defined types, a temporary object then copy construction for the first *may* happen instead of in-place construction.


Actually, this will never happen. MyClass foo = x; is the same as MyClass foo(x); in all cases except for maybe explicit ctors as Bazzy suggested.
Topic archived. No new replies allowed.