6

I'm reading Stroustrups C++ 4th Ed. Page 153 and have questions about initialization vs assignment. It's my understanding that initialization is occurs in the constructor and assignment in operator= overloaded function. Is this correct?

Also, I don't recall seeing the brackets i.e. int count {1} in his 1998 3rd Ed. book. Should I be defining variables like counters using int count {1} or int count = 1? Seems like an awkward difference from C if using the brackets.

Thanks for your guidance

void f() {
   int count {1}; // initialize count to 1
   const char∗ name {"Bjarne"}; // name is a    variable that points to a constant (§7.5) 
   count = 2; // assign 2 to count
   name = "Marian";
}
notaorb
  • 1,944
  • 1
  • 8
  • 18
  • 5
    FTW, a **lot** has changed in C++ since 1998. If your trying to learn modern C++, or just revisiting after some time there is going to be quite a bit of new syntax's/rules/behaviors for you to get used to. – NathanOliver Jun 02 '20 at 20:28
  • 4
    This is because that notation did not exist back then. It was added to the language with C++11 in 2011. Both syntax work. The curly braces are used to denote many different kinds of initialization. I recommend you learn from more recent materiel, specifically post-C++11 as the language and the way of thinking changed drastically. – François Andrieux Jun 02 '20 at 20:28
  • 1
    Initialization happens when a variable is constructed. Assignment happens after construction - to a already initialized variable. – Jesper Juhl Jun 02 '20 at 20:29
  • See [this](https://stackoverflow.com/questions/1051379/is-there-a-difference-between-copy-initialization-and-direct-initialization) for the difference of `T t{val};` and `T t = val;` – NathanOliver Jun 02 '20 at 20:29
  • Does this answer your question? [Initialisation and assignment](https://stackoverflow.com/questions/7350155/initialisation-and-assignment) – GutZuFusss Jun 02 '20 at 20:30
  • The initialization options in modern C++ get a little crazy, so [here's some handy reading](https://en.cppreference.com/w/cpp/language/initialization) and [a video](https://www.youtube.com/watch?v=7DTlWPgX6zs) to help you put it together. – user4581301 Jun 02 '20 at 20:50

2 Answers2

8

The curly braces is part of uniform initialization which was added with the C++11 standard.

Using

int value {1};

is equivalent to

int value = 1;

There's some differences between using curly braces and "assignment" syntax for initialization of variables, but in this simple case they're equal.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Is the copy constructor invoked in the above case? – notaorb Jun 02 '20 at 20:38
  • @notaorb It depends on the type of the variable, the type of the initializer, and the possible available constructor overloads for the variable type. For this specific case, since `int` is a primitive type, it doesn't have any constructors at all. – Some programmer dude Jun 02 '20 at 20:40
1

initialize means you write the variable for the first time and give it an initial value like int x=5; but assignment means that you already had a variable and you change its value like when you come later and set x=10; now you assignment number 10 at variable x

  • 1
    Not completely correct. You've got the right idea, but the wording is misleading. At function scope `int x;` is uninitialized and that makes `x = 1;` the first time it is written to without `x = 1;` being an initialization. – user4581301 Jun 02 '20 at 20:53