4

Reading some code written by a coworker I stumbled on the use of constructor syntax to initialize a primitive type variable. Ie something like below:

#include <iostream>

int main()
{
    using namespace std;

    // initialized using assignement syntax (copy initialisation)
    int titi = 20;
    cout << "titi=" << titi << "\n"; // got 20 in titi, it works

    // initialized using constructor syntax (direct initialization)
    int toto(10);
    cout << "toto=" << toto << "\n"; // got 10 in toto, it works
}

My natural tendency would be to stick with the assignement syntax, as it is the historical one and it's a no brainer, and there is obvious compatibility issues (consructor syntax won't qualify as valid C).

Still I wonder if there is any other non obvious difference between the two syntax ? If they are actually meaning the same thing ? And what are the pros and cons of one or the other form considering for instance future maintenance/code evolution issues or readability issues ?

kriss
  • 23,497
  • 17
  • 97
  • 116
  • 3
    One is copy-initialization and the other is direct-initialization. – chris Jul 17 '13 at 08:48
  • Initialisation and assignment : [here](http://stackoverflow.com/questions/7350155/initialisation-and-assignment) – Armand Jul 17 '13 at 08:51
  • 1
    @p1rox, There's no assignment here. – chris Jul 17 '13 at 08:52
  • Two kind of initialization. By the way, copy initialization is not assignment. – lulyon Jul 17 '13 at 08:56
  • @lulyon: what difference do you see between copy and assignement (beside the word used)? I always called `=` operator assignment operator and it is supposed to change the l-value isn't it ? When using `=` to define reference I call that aliasing, but may be I'm also wrong and I should call that assignment ? – kriss Jul 17 '13 at 09:17
  • @kriss The obvious difference is that assignment is only done after initialization, while copy initialization is Simultaneously done with variable(or object) initailization. Assignment is done by perator=, while copy initialization is not necessarily so. – lulyon Jul 17 '13 at 09:28
  • @lulyon: OK, I believe I get the point. Use `copy constructor` wording when speaking about assignment to a previously uninitialized object (which is allowed to call directly constructor) ; reserve use of `assignment` wording only when changing a previously initialized object. – kriss Jul 17 '13 at 09:53

2 Answers2

3

For simple types such as int, there is no difference. For class types, what you call "constructor syntax" is known as direct initialization, and what you call "assignment syntax" as copy initialization. You cannot use copy initialization unless the class supports copy, so the tendency is to prefer direct initialization (with the caveat that then one must worry about the most vexing parse problem). Some people then argue in favor of the direct initialization syntax everywhere, on grounds of homogeneity: use the same format everywhere.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

The constructor syntax is useful when dealing with templates because you don't know whether the type will be a primitive type or a class.

Vincent
  • 666
  • 7
  • 21
  • while working with templates is there a guarantee that all classes will have copy constructors? – fatihk Jul 17 '13 at 08:57
  • @thomas In what sense? You generally impose constraints on the template argument types, like CopyConstructable and Assignable (if you need those properties); it's almost impossible to write a template that can work with absolutely any class a user could write. (But note that direct initialization doesn't require a copy constructor.) – James Kanze Jul 17 '13 at 08:59
  • @James Kanze, very good point, thanks. – fatihk Jul 17 '13 at 09:02
  • good point, but as the code I'm speaking about is not a template, I guess I have no reason to do that. – kriss Jul 17 '13 at 09:55