r/cpp • • Jul 02 '26

Why use c++ over c for game development?

Isn't c++ overkill for most games? I think with good architectural decisions you can make decent 2d and 3d games using only what's available in c without the higher level concepts introduced in c++? Do you think I'm being stupid here? Am I missing something? I'm only just starting to learn c making simple games leaning heavily on Raylib and ECS architecture.

0 Upvotes

117 comments sorted by

View all comments

Show parent comment

1

u/oracleoftroy Sep 06 '26

What exactly do you mean by this? Because frankly, a lot of the time when I hear the anti-OO crowd say stuff like this, I picture the following meme:

The compiler needs you to to spot the difference between these two pictures:

foo.bar();  

C Programmer: "boo hiss! That's OOP! It's slow, and confusing and no one could possibly tell what it is doing!"

bar(&foo);  

C Programmer: "Yay! That's great code! So fast, and simple. I know exactly what its doing down to the last instruction!"

Assembler: "It's the same picture."

A rather long-standing bit of C++ advice for good class design is: "Prefer non-member non-friend functions to member functions." That's item 23 in Effective C++, 3rd edition, 2005 (Meyers) and item 44 from C++ Coding Standards, 2004 (Sutter, Alexandrescu). I think this originates from Meyer's Dr. Dobbs article "How Non-Member Functions Improve Encapsulation" from 2000. All are very good reads.

So in short, it's been a C++ OOP best practice to not make everything a member function for about 26 years at least. C++ is not like Java or C#, it doesn't force you to put everything in classes. And that's a good thing. Free functions are great.

Moreover, most objects won't have virtual methods, and most should behave like value types and avoid pointer indirections where possible. Objects can encapsulate SoA style data.

To be honest, a lot of the anti-OOP arguments are based on things OOP programmers already recognize as bad practice. But for some reason they seem to think all OOP amounts to deep inheritance hierarchies, run time pointer chasing graphs of data, and all sorts of other things that are simply bad design and independent of OOP or Procedural or Functional, etc. Bad practice doesn't become good because the slow pointer chasing isn't done in a OOP paradigm.

1

u/Randomfreezer Sep 06 '26

Bar(&foo) is also wrong if the function is designed to operate for a single instance of an object. Ideally in data driven C we want functions to work on arrays of structs. This is the number one issue with OOP.

2

u/oracleoftroy Sep 06 '26

Ideally in data driven C we want functions to work on arrays of structs.

I think you have that backwards. You want structures of arrays (SoA) because it works better with SIMD. Although in reality, it's a bit too simple to say SoA is always good and AoS is always bad. If you are going to use every struct member anyway, it usually doesn't make a big difference if it is SoA or AoS.

And not every problem can be reduced to crunching data through SIMD. Though when it can be, that's great. Sometimes you just have one or two of something.

1

u/Randomfreezer Sep 07 '26

Yeah that is what I meant

1

u/Randomfreezer Sep 06 '26

And if you have to prefer non member functions, then that effectively advocates an anti oop standpoint. C++ is multi paragdim it is not OOP. It can be whatever you need it to be.

1

u/oracleoftroy Sep 06 '26

And if you have to prefer non member functions, then that effectively advocates an anti oop standpoint.

Not really. It's just that the anti-OOP side has a comically narrow definition of OOP. Their concerns are usually legit, but usually their advice is already well known to the OOP side.