Obscure C++ Features
31–40 of 58 posts
Re: Obscure C++ Features
#32C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value. if (MouseEvent *mouse = dynamic_cast (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, for example, you'd like to reverse the condition, you can't write
if (!(MouseEvent *mouse = dynamic_cast(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.Re: Obscure C++ Features
#33C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value. if (MouseEvent *mouse = dynamic_cast (event)) I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned.
MouseEvent *mouse;
if(mouse = dynamic_cast(event)))
but it is not the same thing.Re: Obscure C++ Features
#34C++ contains a syntactical shorthand for simultaneously declaring a variable and branching on its value. if (MouseEvent *mouse = dynamic_cast (event)) I think it is a complicated description and a complicated example for saying that the assignment operator (=) returns the value that was assigned.
The noteworthy bit is that declarations are valid in the conditional "expression" of if-statements and evaluate to the declared variable. It's imo not obvious that declarations are expressions sometimes.
Re: Obscure C++ Features
#35Re: Obscure C++ Features
#36Re. the "redefining keywords" trick. I know this works with at least some versions of g++. However when I tried doing it in MSVC 2008 I ended up with link errors between the library object files (compiled without the redefinition trick) and the app executable that did use the trick. It looked like MSVC was encoding the visibility in the mangled method names.
Re: Obscure C++ Features
#37Re: Obscure C++ Features
#38Earlier quoted context omitted.
>The behavior of [] is from C -- it's not a bizarro C++ feature. I'd say that a feature of C++ is still a feature of C++ even if the same feature is in C.
The point was probably that it is not obscure; it's what you had to learn when using plain C in the first week.
Re: Obscure C++ Features
#39Earlier quoted context omitted.
If this were just if (mouse = dynamic_cast (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, no…
I see this as no different than being able to do for (int i = 0; i