Relative Content

Tag Archive for c++constructorcompilationassert

Why assert() condition does not catch undefined variables in a constructor?

as per title, I have created a class with a constructor that also declares some boolean variables.
These variables are not initialised in the constructor, however, I have tried testing their undefined behaviour by adding assert(var == true) after their declaration.
Since the constructor is called several times (>10k) I am expecting the assertion to fail at some point. However, it never does. In addition, I’ve tried changing the condition to assert(var == false) , but still it does not fail.
Why is this? Could it be a compiler thing? I am using g++ 9.4.0.

(C++) Why assert() condition does not catch undefined variables in a constructor?

as per title, I have created a class with a constructor that also declares some boolean variables.
These variables are not initialised in the constructor, however, I have tried testing their undefined behaviour by adding assert(var == true) after their declaration.
Since the constructor is called several times (>10k) I am expecting the assertion to fail at some point. However, it never does. In addition, I’ve tried changing the condition to assert(var == false) , but still it does not fail.
Why is this? Could it be a compiler thing? I am using g++ 9.4.0.
Thanks in advance