Why is C not an object-oriented language?
From the “I wrote this answer on Stack Overflow”-department:
I can write object-oriented code in C with structs and function pointers. Why is it not considered “object-oriented”?
The distinction between an object-oriented language and some other is not whether you can write a program a certain way (they’re all turing complete, after all), but whether it comes with provisions to support this programming style.
In the end, everything in a program compiles down to machine code. By extension, that means you can write code for any programming paradigm in assembler. If you allow me to horribly simplify, C is basically a portable assembler. You can implement all programming styles in C. You may have to resort to macros for a few things, and it may not be as efficient as in assembler, and may involve a lot more manual work, but you can do it.
You will need to re-implement a lot of the provisions for object-oriented programming yourself that come built into C++, if you use C instead:
You need to manually define each implementation function, the virtual method
dispatch tables for each class and subclass, keep them in sync and wildly typecast between
base and inherited classes (or just stay aware of which methods are from the base class
and call them via a super
member or a chain of super.super.super
).
Also, C++ knows what an object is, and can detect nonsensical code that a C compiler looking at your re-implementation of everything wouldn’t be able to tell. It won’t just NULL-initialize a missing function pointer at the end of the vtable and move on, it will tell you that you forgot to implement a pure virtual method, or will fill it in itself because it sees it was added to the base class.
Moreover, an OO language constitutes a standard for OO provisions. Your OO-with-C-code will be different from someone else’s. If it’s built into the language, you can just grab someone else’s OO code and use it. If you use different styles for OO-with-C, you may have to create adapter objects, even for simple things like a string object or a smart pointer.