tags:

views:

180

answers:

7

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

Hello,

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.

Thanks in advance,

Marnix van Rijswijk.

+2  A: 

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
You must know this is a dupe many times over - why reply?
anon
@Neil: Not sure but I reached my cap about 4 hours ago if you're wondering.
Brian R. Bondy
Perhaps cultivate some introspection then, and vote to close this question as a dupe.
anon
@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
@Brian I routinely exhaust my day's close votes within about three hours - right now, I have none left.
anon
@Neil: Sounds like it's time to go for moderator :)
Brian R. Bondy
@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.
anon
@Brian Whups, You did vote to close. Still, my general point stands.
anon
@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
downvote first, then un-downvote if they fix it later
fuzzy lollipop
@fuzzy: No thanks.
Brian R. Bondy
A: 

Google is your friend!

fuzzy lollipop
Not a good answer! SO community is trying to be the google for programmers.... not to point the finger and tell 'em to go to google and learn to figure it out for themselves!
tommieb75
the first result of that search page is far more comprehensive than anyone is going to type out in an answer here. Why litter SO with duplicate information that is dramatically better and more complete somewhere else. Did you down vote all the people that are complaining about this being a duplicate as well. Duplicate of information is duplicate of information where ever it is. SO should be for stuff you CAN'T find on Google, otherwise it is just more noise to sift through.
fuzzy lollipop
+4  A: 

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
Also see some GTK sources, like for example GtkButton, they are simple to understand, how OO can be implemented in C.
Tarantula
@Tarantula GTK uses GObject
Spudd86
That's why I cited it lol.
Tarantula
A: 

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
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.
anon
tomato, tomahto :) it's still a preprocessor.
KevinDTimm
+1  A: 

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.

tommieb75
A: 

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.

Ninefingers