Jump to content
  • Advertisement
Sign in to follow this  
Lucidquiet

Coding C in an OO fashion.

This topic is 4554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've heard its possible, but I don't know the specifics. You can accomplish data hiding (Encapsulation) fairly easilty by with C, but how would someone go about Polymorphism, and Inheritance, with C. (I'm working a on a new language, that is a cross between Python and C). Thanks for any help, L-

Share this post


Link to post
Share on other sites
Advertisement
This is generally a bad idea. It's possible, but extremely limited and messy. The GTK GUI toolkit does this, and it's more or less a nightmare. Regardless, GTK is an example (actually, I think that might be part of GLib), and I know I've read an article about this in Dr. Dobbs Journal.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lucidquiet
I've heard its possible, but I don't know the specifics. You can accomplish data hiding (Encapsulation) fairly easilty by with C, but how would someone go about Polymorphism, and Inheritance, with C.


Polymorphism can be emulated via function pointers. Your C++ compiler will create a table containing all function pointers, then create a hidden pointer to that table in the data area of each instance.

Inheritance can be emulated by having structures (the derived type) contain members of other structures (the base types). So each base type will be a part of an instance of the derived type.

Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).

Share this post


Link to post
Share on other sites
Quote:
Original post by nmi
Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).


Thanks, nmi and jonahrowley.

What about structures, for the name mangling problem, I mean if 2 structures have the same function pointers in them, with same names, there shouldn't be conflict I think...

L-

Share this post


Link to post
Share on other sites
Quote:
Original post by Lucidquiet
Quote:
Original post by nmi
Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).


Thanks, nmi and jonahrowley.

What about structures, for the name mangling problem, I mean if 2 structures have the same function pointers in them, with same names, there shouldn't be conflict I think...

L-


You are right.

On a side note, since "member functions" are implemented as function pointers in C, I don't see why you may need name mangling - just give a diferent name to the new function (C don't allow you to create functiosn with the same name, so it is not really a problem). Did I miss something?

You might also need to play with complex type casting - and in the end, you'll probably end up with something along the line of GLib's GObject, which is pretty heavy.

Regards,

Share this post


Link to post
Share on other sites
LUA might fit somewhere between C and Python.

But seriously, why do you want to create a new language, and why does that mean you need to know how to use OO facilities in C?

Share this post


Link to post
Share on other sites
I love to make new languages, its all I ever do when programming. Making interpreters or compilers is bout all I do lol. Im guessing your writing an interpreter/vm for this? If your making a compiler I dont see why you would need to know how to do OO in C...and why dont you just write the interpreter in C++?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lucidquiet
Quote:
Original post by nmi
Emulation of member functions will require name mangling (to avoid conflicts in case multiple classes define a function with the same name) and having each function expect a pointer to its data as additional parameter (this pointer).

What about structures, for the name mangling problem, I mean if 2 structures have the same function pointers in them, with same names, there shouldn't be conflict I think...


You don't have to mangle names to have structures with members of type pointer-to-function. The mangling comes into play if you want to support things like namespaces or overloading.

An excellent example of object-oriented programming using C is the stdio FILE* functions (fopen(), fread(), fwrite(), ftell(), fclose() etc).

Another example is the entire OpenSSL suite, but not for the faint-of-heart.

Share this post


Link to post
Share on other sites
The first C++ compiler wasn't really a true compiler even, it was some sort of nice preprocessor that converted the C++ code into C code.

So most standard(i.e., not arcane or overly complex) features of C++ are "emulable " in C, in some form or another. Whether it's nice and clean or horribly messy C, that remains a question.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!