Mats said:
I can write
int x(5);
or
int x = 5;
Are both the same ?
For type 'int' - yes. In general case - no. It depends on actual types
involved in initialization.
For example, if the initializer and the object being initialized have
different types (the former - T, the latter - U), then the compiler will
make an attempt to convert the initializer from type T to type U. If
this process involves a conversion construction of the initializer,
which happens to be declared as 'explicit', the second form will fail,
while the first form will still compile:
std::auto_ptr<int> p1 = new int; // ERROR
std::auto_ptr<int> p2(new int); // OK
For another example, if T is different from U and if the conversion can
only be performed by using some intermediate type X in the following
two-step manner
1. convert T to X using T's conversion operator
2. convert X to U using U's conversion constructor
then the second form will fail, while the first form will still compile:
struct T { operator void*() const; };
struct U { U(void*); };
T t;
U u1 = t; // ERROR
U u2(t); // OK
Is either form preferable for some reason I don't
see ?
In situations when you actually have a choice use whatever looks better
to your eyes.