6.4.2 The switch statement
The switch statement causes control to be transferred to one of several statements depending on the value of a condition.
The condition shall be of integral type, enumeration type, or of a class type for which a single conversion function to integral or enumeration type exists (12.3). If the condition is of class type, the condition is converted by calling that conversion function, and the result of the conversion is used in place of the original condition for the remainder of this section. Integral promotions are performed. Any statement within the switch statement can be labeled with one or more case labels as follows:
case constant-expression:
where the constant-expression shall be an integral constant-expression. The integral constant-expression (5.19) is implicitly converted to the promoted type of the switch condition. No two of the case constants in the same switch shall have the same value after conversion to the promoted type of the switch condition.
In your case, your switch condition which is of bool type (because of the boolean operation) is promoted to an integral type, probably int. As such this int would only be able to take on the values 0 or 1 corresponding to false or true. Since you are comparing your case expressions with 0 or 1, depending on their values, one of the case blocks might get executed, but it might not lead to what you might expect
. Your code is perfectly legal C++ but semantic bogus
. I would adopt the code posted by Semicolon or take a look at one of the
many examples of SFML usage,
all of which contain such a switch statement since this is something you have to do in every application you write.
Ok than tell if this would work as well:
...
switch(event.type){
case sf::Event::Closed:
Window.close();
break;
case sf::Event::KeyPressed:
if(event == sf::Keyboard::Escape){
Window.close();
}
break;
}
Just take a look at the
examples... they aren't that hard to understand. You can save yourself a lot of trial and error and even the time of some people who mind replying to this thread...