4

Why C++ gives two ways to initialize variable?

First way is C-type initialization where we assign value to the variable at the place where we define it.

int a = 0;

Another way, constructor initialization which is done by enclosing the initial value between parentheses ().

int a(0);

My question is what was reason that the creators of C++ were forced to introduce new way to initialize variable. Although C-style initialization was doing the job.

HadeS
  • 2,020
  • 19
  • 36

3 Answers3

1

int a = 0; exists for legacy (and because it feels natural, especially for built-in types), and int a(0) exists for explicitness and consistency - there are situations where you may want a more complicated copy constructor which takes multiple arguments or arguments of other types (conversion constructors).

If it can (ie. if the appropriate constructor is available), the compiler will treat both int a = 0; and int a(0) as a call to the copy constructor. The precise behavior is explained here.

I think this is because constructors with initializer lists are generally faster, which I think has to do with the the fact that the value can be placed into the newly allocated variable memory in fewer memory accessing operations. Here is a CPP FAQ on that topic (a great website for questions like this, btw).

stett
  • 1,351
  • 1
  • 11
  • 24
  • 1
    You should clarify that your leading statement is a misconception, otherwise you give the impression that it holds true. – juanchopanza Sep 25 '15 at 09:29
  • @juanchopanza - yes good point. I've updated my answer to make it a bit more direct. – stett Sep 25 '15 at 09:40
  • 1
    I also don't think "the compiler will replace `int a = 0;` with `int a(0);`" is correct. The compiler doesn't have to do that, and the standard doesn't say it has to. – juanchopanza Sep 25 '15 at 09:41
  • "Copy initialization is performed in the following situations: 1) when a named variable (automatic, static, or thread-local) of a non-reference type T is declared with the initializer consisting of an equals sign followed by an expression." - http://en.cppreference.com/w/cpp/language/copy_initialization – stett Sep 25 '15 at 09:42
  • 1
    That doesn't mean the compiler replaces one thing with the other. – juanchopanza Sep 25 '15 at 09:43
  • Hm yes you may be right - my wording is off... I don't know that it "replaces" it, but it treats the one as the other. – stett Sep 25 '15 at 09:44
  • I would say they are *functionally equivalent* if the types involved have certain properties, not that they are replaced or treated as one another. – TartanLlama Sep 25 '15 at 09:46
  • I guess you could say under certain conditions (which apply to OP's example) the two are equivalent. – juanchopanza Sep 25 '15 at 09:46
0

Basically implicit is the preferred way:

int nValue = 5; // explicit initialization

int nValue(5); // implicit initialization

Here are some reads:

http://www.learncpp.com/cpp-tutorial/21-basic-addressing-and-variable-declaration/

Explicit Assignment vs Implicit Assignment

Community
  • 1
  • 1
Wald
  • 1,063
  • 7
  • 13
  • 1
    The terms `explicit initialization` and `implicit initialization` seem mixed up and imprecise to me. The official terms are copy-initialization and direct-initialization. – TartanLlama Sep 25 '15 at 09:32
-4

When you use the first, what you call C-type initialization, the variable will be written to twice, first the default value and second the assignment, as for the second case it will directly be set to the specified value. I think modern compilers optimize this for primitive types but for objects it could make quite a difference.

rockworm
  • 11
  • 2