Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use C as object oriented language?

Tags:

c

oop

There are a lot of excellent answers how can one simulate object oriented concepts with C. To name a few:

  • C double linked list with abstract data type
  • C as an object oriented language
  • Can you write object-oriented code in C?

When is it appropriate to use such simulation and not to use languages that support object-oriented techniques natively?

Highly related:

  • Why artificially limit your code to C?
  • https://stackoverflow.com/questions/482574/whats-the-advantage-of-using-c-over-c-or-is-there-one
like image 538
Roman Byshko Avatar asked Nov 30 '11 22:11

Roman Byshko


2 Answers

I'll give you the one reason I know of because it has been the case for me:

When you are developing software for a unique platform, and the only available compiler is a C compiler. This happens quite often in the world of embedded microcontrollers.

like image 188
e.James Avatar answered Oct 06 '22 02:10

e.James


To just give you another example: a fair amount of the x86 Linux kernel is using C as if it were C++, when object-orientation seems natural (eg, in the VFS). The kernel is written in assembly and C (if that wasn't changed in the 3.0 kernel). The kernel coders create macros and structures, sometimes even named similar to C++ terms (eg, for_each_xxx), that allow them to code as-if. As others have pointed out, you'd never choose C if you start a heavily object-oriented program; but when you're adjusting C based code to add object-oriented features, you might.

like image 38
gnometorule Avatar answered Oct 06 '22 02:10

gnometorule