LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-31-2009, 12:24 AM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
OOP in C


I've been reading through this PDF on how to do object-oriented programming in C (not C++):

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

I was curious if any of you guys out there had ever tried to create an oop system or library for C, or at least had read through that PDF. I only got about a third of the way through. I was understanding it, but it was taking me so long that I finally decided it would probably be better to just use C++.

But then, I'm still something of a novice C programmer, so I thought perhaps some of you guys might have had better experiences.
 
Old 03-31-2009, 01:10 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Unless you have a very compelling reason to mimic the O-O paradigm in C I would recommend that you use C++ or another language that supports O-O if you feel that you require any of the O-O features.
 
Old 03-31-2009, 09:02 AM   #3
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 66

Rep: Reputation: 17
Well in some (tough very few) special cases using plain C is necessary.

If what you need is simply use OOP features then just use C++. If you're curious, take a look at this:
http://www.codeproject.com/KB/cpp/PolyC.aspx
http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html

I would also recommend you to take a look at the FreeBSD kernel sources, networking code is written using polymorphism in plain C and they implemented it in a very simple way. Take a look at the ifnet structure: http://fxr.watson.org/fxr/source/net...FREEBSD62#L112

Last edited by irey; 03-31-2009 at 09:11 AM.
 
Old 03-31-2009, 09:13 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Well in some (tough very few) special cases using plain C is necessary.
Take a look at this:
http://www.codeproject.com/KB/cpp/PolyC.aspx
http://ldeniau.home.cern.ch/ldeniau/html/oopc/oopc.html
I fail to see from either of those articles why it is "necessary".

Edit:
Ok that edit is better.

Last edited by dmail; 03-31-2009 at 09:14 AM.
 
Old 04-01-2009, 03:37 AM   #5
fozner
Member
 
Registered: Aug 2006
Posts: 147

Rep: Reputation: 16
Tiny Object Oriented Dictionary in C

Here is a tiny object-oriented C library for dynamic strings. It works like PHP's variable variables ($$var) and Python dictionaries to give you strings with names that are also strings.

It supports the following methods:

dictionary_add()
dictionary_print()
dictionary_destroy()

Simplified example:

dictionary_add("word","some definition");
dictionary_print("word");//prints "some definition" to the terminal
dictionary_destroy();//please destroy after use.

http://www.thenerdshow.com/?page=dictionary

For other pseudo object-oriented C programs, I refer you to the mess of gtk, glib, glade, gobject and friends.

Last edited by fozner; 04-01-2009 at 03:38 AM.
 
Old 04-02-2009, 05:40 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
What the document describes is how to implement OOP in C. That is not that strange, after all any (compiled) OOP language is compiled into machine executable statements, and there is no reason why such a thing cannot be written in C which also compiles into machine executable statements.

However, ONLY if your goal is to learn how to develop an OO language in C, it might be worthwhile to study this document and follow the implementation.

For all other purposes it should sufficient to use an OOP language if you want to write OO programs. All the work has already been done and implemented in the compiler.

jlinkels
 
Old 04-03-2009, 12:13 AM   #7
Recursion
LQ Newbie
 
Registered: Apr 2009
Posts: 27

Rep: Reputation: 17
I Use OO ideas in kernel development all the time.
 
Old 04-03-2009, 12:32 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
I Use OO ideas in kernel development all the time.
borrowing the ideas of the O-O paradigm is fine but trying to implement some of the concepts in pure C would miss the point, rather than making the code cleaner you would have, as Churchill once said, "a riddle, wrapped in a mystery, inside an enigma"
 
Old 04-03-2009, 03:27 AM   #9
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 66

Rep: Reputation: 17
Quote:
borrowing the ideas of the O-O paradigm is fine but trying to implement some of the concepts in pure C would miss the point, rather than making the code cleaner you would have, as Churchill once said, "a riddle, wrapped in a mystery, inside an enigma"
C++ is sometimes unadvised in kernel mode. That's why the FreeBSD developers did it in plain C. Even microsoft warns about C++ issues in kernel mode: http://www.microsoft.com/whdc/driver/kernel/KMcode.mspx
However this only happens in particular cases that only advanced programmers face, most people should just use OO languages.
 
  


Reply



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
OOP what it is and how :) PB0711 Programming 16 07-11-2006 10:52 AM
OOP Help Please InvisibleSniper Programming 41 09-18-2005 03:19 AM
What is true OOP? tumana Programming 4 09-13-2004 06:51 AM
When to use OOP KptnKrill Programming 10 08-24-2003 01:15 PM
newbie oop Isitme Programming 3 10-10-2001 12:48 PM

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

All times are GMT -5. The time now is 10:42 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
Open Source Consulting | Domain Registration