11 ms·
These days, OO programing is mostly frowned upon. OO added the idea of implementation inheritance to interfaces, and that's mostly a bad idea. Newer languages
by hedora 3y ago
These days, OO programing is mostly frowned upon. OO added the idea of implementation inheritance to interfaces, and that's mostly a bad idea.
Newer languages and modern C++ favor composition instead. You define interfaces that provide some well-defined capability, and then you write things that wrap the interfaces to provide additional functionality. In C++ you can use templates to do this without any runtime overhead. In C, you can either use preprocessor macros (ouch) or you can build your own vtables. The vtable approach adds virtual method invocations all over the place, so it's not zero cost.
The C++ approach actually isn't zero-cost either: You can end up having many copies of the same template logic, which blows up your instruction cache. One of the big innovations of swift was to avoid that. As far as I know, Rust macros and "go generate" produce binaries that are closer to the C++ approach.
Anyway, especially for emulators, you should look into getting good with C++ template meta programming, or just learn rust and then use its implementation of generics. Both approaches will let the compiler inline the heck out of stuff that ends up in the inner loop. In particular, clang + gcc are both good at constant propagation.
One problem with jumping straight from C to rust is that you basically have to already be able to write stable C++ code in order to get it to compile at all. The borrow checker moves subtle but common C++ errors from runtime to compile time. If you have a lot of experience with C already, the jump might be OK.
(edit: I should add that C++ isn't standing still, and there are lots of cool efforts to backport the good ideas from Rust to it. Systems language competition is a good thing, and they're my two favorite languages!)
- jcelerier 3y ago> You can end up having many copies of the same template logic, which blows up your instruction cache. linkers have been able to perform identical-code-folding optimizations for a few decades now
- Jtsummers 3y agohttps://www.tiobe.com/tiobe-index/ https://www.tiobe.com/tiobe-index/ Without having to squint, 12 of those languages are easily categorized as OO languages. Given the mindshare of those 12 languages, I don't think it makes sense to say that "OO programming is mostly frowned upon." [emphasis added] Maybe it's frowned upon by some, maybe even many, but if it's mostly frowned upon then a lot of people must hate their work.
- bmoxb 3y agoThey are, if I'm understanding correctly, referring to what may be considered the 'traditional' OOP style (Animal, which has various properties and behaviours, is extended by Dog, which has its own properties and behaviours, maybe overriding those of Animal) vs a style where inheritance is use primarily to define and implement interfaces (i.e., an expected set of behaviours with no implementation included in the base class). In other words, the different ways a language supporting OOP may be used rather than what the language supports in and of itself.
- AnimalMuppet 3y ago> These days, OO programing is mostly frowned upon. Only in certain circles. For the software world as a whole, "mostly" is rather an overstatement. (For that matter, for the software world as a whole, "mostly" is an overstatement no matter what claim follows the "mostly".) > OO added the idea of implementation inheritance to interfaces, and that's mostly a bad idea. There is more than one flavor of OO. Not all of them support implementation inheritance. You're using a feature of a sub-part to complain about the whole. (In fairness, though, this is a thread about C++...) Current best practice is "prefer composition over inheritance". There are places, though, where inheritance is the correct answer. When you hit those places, use it. Where it's not, don't use it.
- m_nyongesa 3y agoDo you have a recommended link to a book or tutorial that teaches one how to do composition rather than inheritance in C++ (the way you are describing)? Thank you in advance!