Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'd like to ask HN for references explaining how to layout a large program in C, for someone used to thinking with OOP. For example for a moderately complex arcade game, how would I manage all the states, entities, and interactions? I can write any simple C program but this always trips me up.


Basically just like you would in OOP. Put related variables next to each other in a group and pass this group around. In OOP this is called a class and in classless languages such as C this is called a struct. The functions on this object can be organised in modules just like would be done in OOP. See also https://gamedev.stackexchange.com/a/172405.


Think of the Linux kernel. That's a huge project, organized in an object oriented fashion. Is there a difference between this->method(arg) and method(this, arg)?


It'd be more like typename_method(this, arg, &err) in C because there are no namespaces or exceptions. Also you'll have to manually call destructors.

So yeah, it works fine, just more verbose cause it lacks some syntactic sugar. I prefer C for simple or high-performance programs, and I can see why people might want C++ for larger ones. Problem is some people make a mess pulling out all the stops in C++ OOP features.


define structures, define functions that take those structures as parameters. this is basically what OO is all about. but writing it in C++ will be easier.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: