1

Possible Duplicate:
Can you write object oriented code in C?
Object oriented programming in C

So, as I get it, "objects" are basically just wrappers for values and methods. Can't you have the same functionality in C, with structs? A struct seems just like a simple class, but, of course, it doesn't have any methods. Here we get to the heart of my problem: I don't understand why methods are needed at all. Wouldn't it be simpler and more memory-efficient if we had an outside function, which just accepted a pointer to an instance of a struct? Or even have the structs have pointers to these functions, but this seems purely aesthetic...

Community
  • 1
  • 1
corazza
  • 31,222
  • 37
  • 115
  • 186
  • 6
    Many duplicates, e.g. [Object oriented programming in C](http://stackoverflow.com/questions/2181079/object-oriented-programming-in-c), [Can you write object oriented code in C?](http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c), [Object Oriented pattern in C?](http://stackoverflow.com/questions/1201521/object-oriented-pattern-in-c), [C as an object oriented language](http://stackoverflow.com/questions/2352413/c-as-an-object-oriented-language) – Paul R May 21 '12 at 08:33
  • Indeed, not tying methods to classes means you can have generic functions which can be specialised on multiple arguments, not just "`this`"—we start getting to [CLOS](http://en.wikipedia.org/wiki/Common_Lisp_Object_System)'s [generic functions](http://en.wikipedia.org/wiki/Generic_function) (which have nothing to do with things by similar names in other languages, save Dylan). – Asherah May 21 '12 at 08:38
  • 1
    *Wouldn't it be simpler and more memory-efficient if we had an outside function, which just accepted a pointer to an instance of a struct?* Yes, that is exactly what happens in C++ when you add member functions to a struct. The compiler moves them "outside" and adds a `this` pointer to the call. – Bo Persson May 21 '12 at 09:01
  • The languages which you think of as *object-oriented* (Java, Python, Ruby) are all mostly implemented in C, so yes, of course C can be object oriented. That does not mean that it is easy or convenient though. – jforberg May 21 '12 at 09:08

4 Answers4

6

Being object-oriented means being object-oriented out of the box. Sure you can simulate object orientation with C (and with many other non-OO languages), but if the language (and/or its standard library) does not help you with that in any way (special syntax, standard objects, etc) and does not encourage to write in OO style by default, it will not be called object-oriented.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
4

Using c-style function pointers as struct members, you can indeed make C object oriented.

I like the concept of having a class with its attributes and methods all defined together. Its easier to see what the related entities are, as opposed to having separate functions that take pointers to the struct like you mention.

Here are 2 related SO questions:

Is there a simple way to code the strategy (or other) design pattern in ANSI C that would fit on the screen of an 11" MacBook Air?

How do function pointers in C work?

Community
  • 1
  • 1
Brady
  • 10,207
  • 2
  • 20
  • 59
2

What you suggest is already used to implement OO features in C. But many people find easier to use dedicated languages where OO features are included.

This is the same trend as why people switched to C from assembly.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
-2

Object is real-world entity and it has state, behavior. In class(c++/java) we can define state as member variables and functions as behavior. In case of C: in struct, you may have state but not behavior. Therefore C is not object oriented. This is just a small e.g. Also C does not support OOPS principals like inheritance, function overloading, polymorphism etc.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85