bool zu numerischen typ
-
Hi,
ich habe einen bool b und will den in verschiedene Typen konvertieren. Wenn b true is, soll in der Variable 1 stehen, ansonsten 0.
Kann ich da einfach zuweisen a la int foo = b; oder brauch ich da eine Konvertierung wie
int foo = b ? 1 : 0;
?
-
Probiers doch aus

-
jup das Funktioniert
int i = 0; bool b = 0; if(i == b) { b = true; } i = b; std::cout<<i;Da bekommst du als Textausgabe 1.
-
Das ist auch durch den Standard zugesichert.
open-std.org ( §4.7) schrieb:
If the source type is bool, the value false is converted to zero and the value true is converted to one.