LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2009, 05:14 AM   #1
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 66

Rep: Reputation: 17
implementing OO concepts in plain C


Hi,

I've seen lots of tutorials that explain how to implement inheritance an polymorphism in plain C. Most of them do this through function pointers.

However I'd like to support multiple inheritance. Does anyone know about a book or article that explains this?

Thanks in advance
 
Old 03-20-2009, 06:07 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
What's the big deal / point ?

AFAIK "OO" in "C" is implemented through structures which store the object state.
Methods are in global namespace anyway, so prefixes are used.

So, if you want your "class" to use a function from another "class", you just call it.

If you want data from another "class" to be used, then pass to your function the needed state structures or parts thereof.

...

But the bigger point/question is: what's the point of OO in "C" in the first place ?
 
Old 03-20-2009, 06:42 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by Sergei Steshenko View Post
What's the big deal / point ?

AFAIK "OO" in "C" is implemented through structures which store the object state.
Methods are in global namespace anyway, so prefixes are used.

So, if you want your "class" to use a function from another "class", you just call it.

If you want data from another "class" to be used, then pass to your function the needed state structures or parts thereof.
What you describe is merely "good practices" for programming in C. Or, at best, the first step towards OOP in C. By no means the whole story about OOP.

Quote:
But the bigger point/question is: what's the point of OO in "C" in the first place ?
Good point. I don't know the answer really. Yet I assume there is a point to make about OO programming in plain C, since e.g. the GTK-libs (the base for gnome, gimp,..) are written this way.
 
Old 03-20-2009, 07:25 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Hko View Post
What you describe is merely "good practices" for programming in C. Or, at best, the first step towards OOP in C. By no means the whole story about OOP.

Good point. I don't know the answer really. Yet I assume there is a point to make about OO programming in plain C, since e.g. the GTK-libs (the base for gnome, gimp,..) are written this way.
No, gtk+ is not written in OO way, it's written in a way that imitates OO in the manner I described.

And a very popular type in gtk+ is void *.
 
Old 03-20-2009, 07:26 AM   #5
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by irey View Post
Hi,

I've seen lots of tutorials that explain how to implement inheritance an polymorphism in plain C. Most of them do this through function pointers.

However I'd like to support multiple inheritance. Does anyone know about a book or article that explains this?

Thanks in advance
Try this paper (might be heavy reading):
http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html

Looks doable, but it will take some time to get used to (you'll probably need few hours to build several toy examples and think about it), you might want to wrap it within few macros, there will be a slight overhead per every method call and certainly single-inheritance will be easier to implement.

Quote:
Originally Posted by Sergei Steshenko View Post
But the bigger point/question
is: what's the point of OO in "C" in the first place ?
I don't think this is really important thing to know.
But I can imagine a platform where only C compiler is available, or maybe there is simple interest in "how can this be done". Anyway, "why?" isn't important. If a person wants to use multiple inheritance in C, "Why?" is not my problem.
 
Old 03-20-2009, 07:34 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ErV View Post
...
But I can imagine a platform where only C compiler is available
...
Not an issue - LLVM translates C++ into plain "C".

And with statement above again - why ? I.e. why to bother if there is a straightforward way to achieve better results with smaller effort ?
 
Old 03-20-2009, 07:50 AM   #7
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Sergei Steshenko View Post
And with statement above again - why ? I.e. why to bother if there is a straightforward way to achieve better results with smaller effort ?
To learn how it works or how it can be implemented, for example. Or "just for fun", "out of boredom", "to get some challenge", "because it gives full control", "to try something new", "to learn something new" and so on. There are quite a lot of reasons why someone might want to do that (or why I would do that).
Anyway, this technique might be a useful thing to know (remember that Qt made their own preprocessor on top of C++). So I don't think "why?" is important. If someone asks exact question, it will be simpler to give exact answer instead of discussing motivation.
 
Old 03-20-2009, 07:58 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ErV View Post
...
(remember that Qt made their own preprocessor on top of C++)
...
So what TrollTech is doing is _not_ just C++, it's preprocessor + C++.

If we are talking about preprocessor + "C", it's a different thing.

And C++ + LLVM is a preprocessor + "C", i.e. C++ is used as preprocessor language, and LLVM is used as preprocessor tool.
 
Old 03-20-2009, 08:25 AM   #9
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Sergei Steshenko View Post
So what TrollTech is doing is _not_ just C++, it's preprocessor + C++.
Is this a question?

Quote:
Originally Posted by Sergei Steshenko View Post
If we are talking about preprocessor + "C", it's a different thing.
Look, I explained why someone would want to implement multiple inheritance in C, and I don't see a problem.
 
Old 03-20-2009, 08:29 AM   #10
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 66

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by ErV View Post
Try this paper (might be heavy reading):
http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
Thanks. It looks useful.

The reason for implementing it in plain C is that there is no portable solution to implement C++ classes in a shared library. I know it's possible and it works, but libraries made with one compiler are not guaranteed to work with others.

Last edited by irey; 03-20-2009 at 08:30 AM.
 
Old 03-20-2009, 08:30 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ErV View Post
Is this a question?


Look, I explained why someone would want to implement multiple inheritance in C, and I don't see a problem.
The OP mentioned pure "C", so I'm not sure whether preprocessor is an acceptable option for him.

Another point is that in pure "C", without any attempt to imitate OO, one already has "multiple inheritance" - any non-hidden data and any non-hidden function can be used by any other function.
 
Old 03-20-2009, 08:33 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by irey View Post
Thanks. It looks useful.

The reason for implementing it in plain C is that there is no portable solution to implement C++ classes in a shared library. I know it's possible and it works, but libraries made with one compiler are not guaranteed to work with others.
As I mentioned, it is possible to translate C++ into "C" using, say, LLVM.

Then you compile the "C" code with compiler of your choice.
 
Old 03-20-2009, 08:56 AM   #13
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17
Quote:
Originally Posted by irey View Post
Thanks. It looks useful.

The reason for implementing it in plain C is that there is no portable solution to implement C++ classes in a shared library. I know it's possible and it works, but libraries made with one compiler are not guaranteed to work with others.
Why do you say this?
Provide an abstract base which the library class inherits from. Create a factory function and a delete function(although there are methods of being able to use delete) which you decorate as extern C and Bob is your aunty. This will work with all compilers and I even use it on windows in a dll.
 
Old 03-20-2009, 09:02 AM   #14
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Sergei Steshenko View Post
The OP mentioned pure "C", so I'm not sure whether preprocessor is an acceptable option for him.
I didn't suggest to write preprocessor. I mentioned macros only because it will make certain constructs shorter and will shorten time you need to write things.

Quote:
The reason for implementing it in plain C is that there is no portable solution to implement C++ classes in a shared library.
Simplest way to make "Class"(as you probably already know) in a shared library is to make creation function which returns "something" (can be void* typedeffed into something else or pointer to empty struct, etc), then declare methods that take this "something" as first argument. Check OpenGL/WinAPI/ODE etc. This doesn't allow making derivatives.

However, if you want to allow user to pass their class into library it will be easier to declare "interface" - a struct which contains pointers to methods(which take pointers to struct OR pointer to data as a first argument) and (optionally) void* pointer (to user data), where pointers to methods can be specified by user. AFAIK this is how things work in windows COM interfaces - the class is stored in whatever form, but it provides "interface" to the program, and one class might support several interfaces. I really don't see the need for implementing multiple inheritance here. Similar approach was used in 3dsmax and somewhat similar approach was used in Maya.

You might want to check some very basic tutorials on windows COM model for this - although this technology is a bit bloated (and my knowledge about it is a bit rusty), it might give you ideas how to simplify/implement things. Although idea of multiple "correct" inheritance might look neat it (IMO) will be much easier to stick to interfaces.

Both approaches are portable.

Last edited by ErV; 03-20-2009 at 09:05 AM.
 
Old 03-20-2009, 09:08 AM   #15
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Biddle View Post
Why do you say this?
Provide an abstract base which the library class inherits from. Create a factory function and a delete function(although there are methods of being able to use delete) which you decorate as extern C and Bob is your aunty. This will work with all compilers and I even use it on windows in a dll.
1) The question is about C, not C++.
2) In C++, there is no warranty that abstract class will be compatible with different compilers, especially if library is closed-source. How are you going to call virtual methods? That's why question is about C.

Last edited by ErV; 03-20-2009 at 09:11 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Concepts lincoln.runwal Linux - Newbie 6 12-16-2008 09:15 PM
USB Concepts nesta Linux - Hardware 2 02-28-2007 08:31 PM
IP layer concepts linetnew Linux - Networking 2 04-18-2005 08:57 AM
Installation concepts plnelson Linux - Newbie 5 10-11-2004 07:24 AM
harddrive concepts balanagireddy Linux - Hardware 2 09-01-2003 10:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration