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

-2

u/HolidayNo84 Jul 03 '26

I don't like the tool.

5

u/therealjerseytom Jul 03 '26

I think this is an opportunity to step back and really ask yourself why you "don't like it", and if there's some perspective that you might be missing.

Different approaches in programming, are to me, literally like you'd look at a hammer, screwdriver, wrench, or saw. I don't have particular sentiments towards any one of them. Like, "Ugh, screwdrivers just suck." They're all good at certain things and not good at others.

If all you've ever been doing is pounding nails, a screwdriver probably seems like it's not worth bothering with.

That all to say, it's worth keeping an open mind to.

1

u/HolidayNo84 Jul 03 '26

I do understand and agree with your message. However I have developed a preference for composition over inheritance because of how easy it is for me to reason about. I find that concepts explained in a functional paradigm are easier for me to learn. Obviously this is entirely personal preference and some people probably have the opposite experience. I think both experiences are entirely valid.

6

u/oracleoftroy Jul 03 '26

Good news! "Favor composition over inheritance" has been an OOP mantra for at least a quarter century!

I don't know why OOP only seems to mean inheritance these days, since many objects don't use or need it at all to provide useful functionality. Inheritance is great to use when you need it and should be used. But it really isn't something one needs all that often. Even in a fully OO project, you may only need it in a handful of places.

1

u/[deleted] Sep 06 '26

OOP has been pushed because on paper it looks like a logical paradigm, ideal for architectural design. Yet it is quite the opposite, for games OOP is a very poor design choice.

Ofcourse it works for average games and for the average problem it feels natural because they haven't learned how to think of that design in different ways.

By bundling data and logic OOP by its design breaks one of the most fundamental laws of programming which is separation of concern.

Once you start exploring you'll see that there are better ways to the mountain.

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/[deleted] 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/[deleted] Sep 07 '26

Yeah that is what I meant

1

u/[deleted] 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.

2

u/Raknarg Jul 07 '26

whys that? Do you just mean you hate inheritance, or what? Cause just the concept of "grouping related information together with transforming member functions" which is more or less the core of OOP is kinda just integral in most languages, C used to be considered "object oriented" it was just very limited in terms of actual polymorphism. You'd even have single-parent non-virtual inheritance by having the first member of your class the parent object.

If you hate inheritence, join the club. I avoid inheritance when I can, in most cases my objects tend to be glorified structs with constructors and maybe handy member functions and a destructor if needed, and polymorphism is achieved through composition or templates.