3 ms·
You can get quite far treating C++ as C with classes, and I recommend this approach for anyone trying to learn it. Especially nascent gamedevs. You’ll be able t
by sillysaurusx 3y ago
You can get quite far treating C++ as C with classes, and I recommend this approach for anyone trying to learn it. Especially nascent gamedevs. You’ll be able to learn the other details as you go.
One tip: template metaprogramming can consume your life. Avoid it, but use templates sparingly; they do simplify code, especially for vector types.
The key with C++ zen is “all things in balance.” You can go too far with basically every aspect of the language. And it’s important to go too far, so that you know why not to.
- ascar 3y ago> You can get quite far treating C++ as C with classes I'm teaching a HPC (threading/openmp/mpi) class and that's exactly what we do. We switched from regular C to C++ for classes and some standard library data structures and that's that. It makes our lifes easier and stuff is still "easy" enough to understand and work with. I've once had to work with a cryptography codebase using excessive templating. It was an absolute nightmare to deal with and the only person I know of able to understand the codebase was the original author.
- sumtechguy 3y agoUsing templates that way is totally possible. The problem is the nesting you end up with makes it hard to follow what is going on as you discovered. I like to use templates to replace #define style programming. As it makes it easier to debug what is going on and get the compiler to work for you instead of in the background and hoping it spits out the right thing to feed to the next stage in the compiler pipeline. Many languages have these sort of 'yeah you can do that but you probably shouldnt' things. C++'s version of that is templates (for a long time it was giant trees of classes). You get sucked into how cool it is then realize you have made a horrible mistake and made a giant mess that you can barely comprehend much less debug.
- ho_schi 3y agoI don’t consider template-metaprogramming as official core language feature, is is an side-effect of templates. Generic-Programming itself with templates is great and the superpower of C++. Readability and maintainability are the key features of good source-code. Therefore - I back off if someone tries to make the code itself “art”. The result shall be reliable craftsmanship. A usual, apply reasonable exceptions ;)
- gpderetta 3y agoSomeone's advanced metaprogramming is someonelse trivial use of templates.
- flohofwoe 3y agoI would call this sort of code "C with namespaces and templates". Classes are definitely the least useful feature that C++ added on top of C.
- gpderetta 3y agothis is trivially true as classes are exactly the same as struct except for the default access. If you mean member functions, virtual functions and inheritance, I beg to differ.
- curt15 3y agoHow else would people do OOP in C? Use GObject and its macros and boilerplate?
- pokeymcsnatch 3y agostructs?
- JohnFen 3y agoThat's how I did it before C++ existed. Structs with pointers to functions (which is conceptually what C++ classes are).
- pokeymcsnatch 3y agoYarp, I do the same. It's rudimentary but works fine and the code is clean enough if you pick a couple conventions (starting "private" variables with an underscore for example) and stick with them.