by bnegreve · 13y ago · 0 comments
if (MouseEvent *mouse = dynamic_cast<MouseEvent *>(event))
I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned. if (mouse = dynamic_cast<MouseEvent *>(event))
then it would indeed just be an if condition expression that happened to be an assignment.But the variable is being declared as well, so what's actually going on here is a declaration and an initialiser expression. There is actually new functionality here that needed a bit of language design thought, e.g., the declared variable's scope is the whole if statement, not just the condition expression.
for (int i = 0; i < len; ++i) { }If, for example, you'd like to reverse the condition, you can't write
if (!(MouseEvent *mouse = dynamic_cast<MouseEvent *>(event)))
{}
because a definition is not a general expression, but was only added as an "extra" to allow its use in some cases in conditionals. MouseEvent *mouse;
if(mouse = dynamic_cast<MouseEvent *>(event)))
but it is not the same thing.