Is there a way to write OO-like code in the C programming language?
See also:
Found by searching on ‘[c] oo’.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first C++ compiler (‘C with classes’) would actually generate C code, so that’s definitely doable.
Basically, your base class is a struct; derived structs must include the base struct at the first position, so that a pointer to the ‘derived’ struct will also be a valid pointer to the base struct.
The functions can be part of the structure as function pointers, so that a syntax like p->call(p) becomes possible, but you still have to explicitly pass a pointer to the struct to the function itself.