-2
bool a{ false };

Now I created a boolean variable, but if I want to do this:

a { false };
a = false;

The first method doesn't work, what's the reason?

hoodlum
  • 11
  • 2
  • 4
    Because you're not *initializing* `a` in that expression. And in the last expression, you're assigning. – cigien May 13 '20 at 17:23
  • You may want to take a look at https://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat – kesarling He-Him May 13 '20 at 17:44
  • Because when you "override" it, you are not initializing it anymore! You can only initialize it once. – stackoverblown May 13 '20 at 18:28

1 Answers1

2

You can only do uniform initialization (or any form of initialization) when an object is created. After an object is initialized, you can only modify it:

bool a{false}; // brace initialization
bool b = false; // copy initialization

// a and b are created now, and can't be initialized again

a {false}; // not valid syntax
b = false; // assignment, NOT initialization
cigien
  • 57,834
  • 11
  • 73
  • 112
  • *"You can only do uniform initialization (or any form of initialization) when you create an object."* I don't think that's correct. `int a; /* <-uninitialized */ a = 0 /* <-now it's initialized*/`. – Aykhan Hagverdili May 13 '20 at 18:28
  • 1
    @Ayxan - Nope, it's still assigned. You example has it default-initialized (with an indeterminate value). – StoryTeller - Unslander Monica May 13 '20 at 18:32
  • @StoryTeller-UnslanderMonica Then why we call `int a;` uninitialized? – Aykhan Hagverdili May 13 '20 at 18:33
  • Isn't the `int` initialized anyway? It's only that the *value* is indeterminate. I think calling it *uninitialized* is technically incorrect. – cigien May 13 '20 at 18:33
  • 2
    @Ayxan - We don't. Not when talking pedantically. Colloquially one may say it's uninitialized. But in the C++ model, no object comes into existence uninitialized (even if said initialization is a no-op). – StoryTeller - Unslander Monica May 13 '20 at 18:35
  • @StoryTeller-UnslanderMonica I thought default initialization was like zero initialization. Thank you for correcting. Where can I learn more about the C++ object model? – Aykhan Hagverdili May 13 '20 at 18:37
  • 2
    @Ayxan - I think cppreference has good articles about the various initialization types in C++. You can also start at http://eel.is/c++draft/basic.life#1 - but delving into [dcl.init] is not for the faint of heart. – StoryTeller - Unslander Monica May 13 '20 at 18:39
  • @StoryTeller-UnslanderMonica one more question. If I do `malloc(100)` am I given a pointer to uninitialized memory or default initialized memory? I don't think a block of memory is an object, but yeah. – Aykhan Hagverdili May 13 '20 at 18:45
  • 1
    [The aforementioned cppreference docs on initialization](https://en.cppreference.com/w/cpp/language/initialization) – user4581301 May 13 '20 at 18:45
  • 2
    @Ayxan - That was actually the $50,000 question for a long time. It was recently resolved with the adoption of https://wg21.link/p0593 – StoryTeller - Unslander Monica May 13 '20 at 18:48
  • 1
    [More cppreference docs](https://en.cppreference.com/w/cpp/language/object) on what is and isn't an object. – user4581301 May 13 '20 at 18:51