Possible Duplicates:
Can you write object oriented code in C?
Object Oriented pattern in C ?
I remember reading a while ago about someone (I think it was Linus Torvalds) talking about how C++ is a horrible language and how you can write object-oriented programs with C. On having had time to reflect, I don't really see how all object oriented concepts carry over into C. Some things are fairly obvious. For example:
- To emulate member functions, you can put function pointers in structs.
- To emulate polymorphism, you can write a function that takes a variable number of arguments and do some voodoo depending on, say, the
sizeof
the parameter(s)
How would you emulate encapsulation and inheritance though?
I suppose encapsulation could sort of be emulated by having a nested struct that stored private members. It would be fairly easy to get around, but could perhaps be named PRIVATE
or something equally obvious to signal that it isn't meant to be used from outside the struct. What about inheritance though?