2

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

I'm wondering if it's possible to use strict ANSI C as a object oriented language. And if it's possible, how do I make a class in ANSI C. Allthough the language isn't designed for OO, I'm really eager to try this.

Any examples, links etc. are appreciated.

user513951
  • 10,578
  • 7
  • 59
  • 72
Marnix v. R.
  • 1,558
  • 3
  • 21
  • 32
  • About a million dupes, including http://stackoverflow.com/questions/2181079/object-oriented-programming-in-c –  Jun 18 '10 at 19:26
  • Curious why you would want to do this when there is C++? – Byron Whitlock Jun 18 '10 at 19:27
  • Yeah, there's no point in doing this when you can just use C++. – Puppy Jun 18 '10 at 19:28
  • 1
    Why is C++ always the answer for this. Maybe the person actually wants to use ANSI C. You could just as easily say there is Objective-C. – BobbyShaftoe Jun 19 '10 at 04:42
  • See also: http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c http://stackoverflow.com/questions/415452/object-orientation-in-c – Dinah Jun 18 '10 at 19:25
  • So-o (Simply object-oriented) - https://www.so-o.org - defines a functional layer which adds an object-oriented programming model to a structured programming language. Inspired by Smalltalk and Objective C, So-o is complete, simple and light, easy to understand. So-o has 3 functions: defclass which defines a new class, sendmsg which is systematically used to send a message to a class or an instance, and supersend which runs a method inherited from a superclass. A game of Poker written in C with So-o : http://www.so-o.org/en/article/poker-in-c. – frasq Jul 27 '18 at 18:48

5 Answers5

6

C doesn't have direct support for OO via classes, but you can easily emulate it.

The basics of how to do this is that you can make a struct which holds your data members and an associated list of functions which takes a pointer to that struct as it's first parameter.

More information

Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
  • You must know this is a dupe many times over - why reply? –  Jun 18 '10 at 19:28
  • @Neil: Not sure but I reached my cap about 4 hours ago if you're wondering. – Brian R. Bondy Jun 18 '10 at 19:32
  • Perhaps cultivate some introspection then, and vote to close this question as a dupe. –  Jun 18 '10 at 19:37
  • 1
    @Neil: Just out of curiosity it doesn't show that you voted to close, but I do see the comment above. Why write a comment and not vote to close? – Brian R. Bondy Jun 18 '10 at 19:45
  • @Brian I routinely exhaust my day's close votes within about three hours - right now, I have none left. –  Jun 18 '10 at 19:47
  • @Neil: Sounds like it's time to go for moderator :) – Brian R. Bondy Jun 18 '10 at 19:49
  • @Brian No, I'm obviously temperamentally unsuited for it. But I do think that people like you who don't downvote often and do knowingly reply to very obvious dupes (of course, we all reply to some dupes to some extent) are not doing SO any favours. –  Jun 18 '10 at 19:53
  • @Brian Whups, You did vote to close. Still, my general point stands. –  Jun 18 '10 at 19:55
  • 2
    @Neil: The responses to the dupes themselves is not doing SO any favors, agree. But "people like me" I wouldn't say are not doing SO any favors. Anyway thanks for the suggestion, I'll try to be more conscious of responding to dupes. About the lack of downvotes, I prefer to tell people in a comment why they are wrong and give them a chance to fix first. – Brian R. Bondy Jun 18 '10 at 20:00
  • downvote first, then un-downvote if they fix it later –  Jun 18 '10 at 20:03
4

It's certainly possible, although I don't like to do it. One of the popular ways of object-oriented C can be found in the GObject architecture used in Gnome. The article (and further reading about GObject) should give you some ideas:

http://en.wikipedia.org/wiki/Gobject

Reinderien
  • 7,724
  • 3
  • 37
  • 65
  • Also see some GTK sources, like for example GtkButton, they are simple to understand, how OO can be implemented in C. – Tarantula Jun 18 '10 at 19:25
3

A struct can hold methods and variables such that

struct myStructFoo{
    int fooBar();
    int privFooBar;
};

That is how you derive an OO "thing" using a plain old ANSI C compiler, if you want to learn the full OOP with C++ you will fare better with an ANSI C++ compiler as that is a better fit for your needs...as the OOP style using a C language is.... look at it this way, sure you can use a object using a struct, but the name is not exactly...intuitive...as a struct is more for holding fields and is part of integral data structures such as linked list, stacks, queues etc. If you had an ANSI C++ Compiler, this is how it would look:

class myFoo{
  public: 
     int fooBar();
  private:
     int privFooBar;
};

Compare and see how it appears more intuitive, information hiding is specified via the public and private keywords.

t0mm13b
  • 32,846
  • 7
  • 71
  • 106
1

There is in fact an effort underway called the OOC language to write a language like C that is object orientated. It is slightly different to C and therefore isn't Objects in C at all, and personally I've never used it - it diverges too far from C for my taste, but it might be worth a look.

It does, interestingly, translate "OOC" to C before compilation. It might be worth a look at how it achieves this as it will effectively be converting objects to C. I suspect this will be done as other posters have mentioned (struct pointers etc) although again I haven't looked at it.

0

sort of. remember that C++ and objective-C were both handled at one time with preprocessors to the C compiler. in the end, you'll just be rewriting c++

KevinDTimm
  • 13,876
  • 3
  • 39
  • 59
  • Whether or not this is true depends on how you define "preprocessor". The cfront C++ compiler was a true compiler that emitted C as its object code. It was not a macro preprocessor like (for example) the C preprocessor. –  Jun 18 '10 at 19:35
  • 1
    tomato, tomahto :) it's still a preprocessor. – KevinDTimm Jun 18 '10 at 19:51