1

I read the following code in c++:

bool result(false);

is result initialized as false?

but are there any benefits to do it instead of:

bool result = false;

can anyone help?

user2131316
  • 3,111
  • 12
  • 39
  • 53
  • 2
    They're both initialization. Direct- and copy-, respectively. – jrok Aug 28 '13 at 09:27
  • what I mean is there are two different forms, are there any difference and any benefits to use one instead of using another one? – user2131316 Aug 28 '13 at 09:28
  • 3
    Yes, there are subtle differences, see here: [Is there a difference in C++ between copy initialization and direct initialization?](http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati) – jrok Aug 28 '13 at 09:29
  • Also, the first can't obviously be used in cases where you actually need assignment. In any case, I'd argue that the latter form is much more common for primitive types. Note that there is a difference between initialization and assignment in places where it might not be apparent immediately. For instance, when you assign values to member variables of a class inside its constructor. Instructions inside the constructors body will be executed after all members have been either explicitly (via initializer list) or default initialized (possibly with garbage). – thokra Aug 28 '13 at 09:35
  • 2
    @thokra there's no assignment going on here. – jrok Aug 28 '13 at 10:03
  • If you initialise it with a default boolean `bool()` then the first becomes a function declaration, whereas the second is unaffected. – Peter Wood Aug 28 '13 at 10:07
  • @jrok: I didn't say there was. – thokra Aug 28 '13 at 10:14
  • @thokra then I misread your comment, apologies. – jrok Aug 28 '13 at 10:18

0 Answers0