When I am presented with programming problems, I naturally start breaking them up into logical objects in my head. Who has what responsibility, who owns what, who derives from what, etc.
I am struggling with C. I just don't get how to do things in a Procedural Language.
Can an experienced C programmer help explain how I should think about my programs during design time?
For example, I want to write my own Semaphore class. I would naturally need a Queue data structure for my program, which I would like to write myself as well. If I needed to do this in Java or C#, I could simply whip up a quick Queue class and create a new instance of it in my Semaphore class.
But in C, there aren't objects. So do I have to inline all the behavior of my Queue data structure?
Can someone help me "get it"?
Related: what is the best way to plan and organize development of an application in c
But in C, there aren't objects. So do I have to inline all the behavior of my Queue data structure?
No.
Do this.
Define your class however you feel comfortable doing OO design.
Write the attributes of your class as a C-language struct.
Put that struct in a header file, along with all of the functions that operate on that struct. Make sure a MyStruct * self
is the first argument to all of these "method functions".
Write a C module with all of the bodies of the method functions.
Poor-person's OO in C. It works well. Just be disciplined about putting everything into the struct that you need -- public and private instance variables -- everything.
Generally, avoid trying to have private variables in the first place. You don't have the full power of an OO compiler, so don't bother with low-value features like "private" or "protected".