The enum example in the article
Re:Why Rust?
by HarHarVeryFunny on Tuesday October 18, 2022 @04:51PM
(#33253736)
OK, that's an interesting language feature, but I can't say it's something that I've often wanted to do in C++. Of course C++ does have std::variant and std::any, although I'm not really a fan. I'd tend to (and have) just use a class that contains the variants and an enum to track which one, then setters and getters.
event.SetKeyPress(ch);
event.SetMouseButtonPress(pos, but); etc
...
switch (event.type) {
case EvKeyPress: ch = event.GetKeyPress();
Re:Why Rust?
by HarHarVeryFunny on Wednesday October 19, 2022 @07:30AM
(#33260577)
Of course the Rust features being used by that Event type are runtime polymorphism and type introspection, so another obvious C++ approach would be just to use a pointer to a virtual based class including the type, then:
switch (event->type) {
case EvKeyPressEvent:
{
auto kpe - dynamic_cast<KeyPressEvent*>(event)
...
}
break;
}
I'm curious to see an example. Could you show something written in Rust, or another "much higher level language" that you think would be messier or more difficult to write in C++ ?