Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Object-oriented programming in standard ANSI C

Reply
Thread Tools

Object-oriented programming in standard ANSI C

 
 
Thierry Chappuis
Guest
Posts: n/a
 
      11-15-2006
Hi,

I'm interested in techniques used to program in an object-oriented way
using the C ANSI language. I'm studying the GObject library and Laurent
Deniau's OOPC framework published on his web site at
http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is
very instructive. I know that I could do much of this stuff with e.g.
C++, but the intellectual challenge of implementing these concepts with
pure ANSI C is relevant to me.

Are you aware of another approaches? Any experience in using such
techniques in production code? The use of GObject seems to be well
implemented in the GNOME world, but I didn't find much about Laurent
Deniau's OOPC. Have you some comments about the strengths and drawbacks
of such techniques?

Many thanks for your valuable help and comments

Best regards

Thierry

 
Reply With Quote
 
 
 
 
Cong Wang
Guest
Posts: n/a
 
      11-15-2006


On Nov 15, 6:28 pm, "Thierry Chappuis" <(E-Mail Removed)> wrote:
> Hi,
>
> I'm interested in techniques used to program in an object-oriented way
> using the C ANSI language. I'm studying the GObject library and Laurent
> Deniau's OOPC framework published on his web site athttp://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is
> very instructive. I know that I could do much of this stuff with e.g.
> C++, but the intellectual challenge of implementing these concepts with
> pure ANSI C is relevant to me.
>
> Are you aware of another approaches? Any experience in using such
> techniques in production code? The use of GObject seems to be well
> implemented in the GNOME world, but I didn't find much about Laurent
> Deniau's OOPC. Have you some comments about the strengths and drawbacks
> of such techniques?
>
> Many thanks for your valuable help and comments
>
> Best regards
>
> Thierry


Yeah. This is an interesting topic. In fact, C can NOT fully support
OOP like C++, but we can use some tricks to make it *like* an
object-oriented programming language. For example, we can use
structures to replace classes, use function pointer as a member of a
structure like a method of a class. And in fact, in Linux kernel source
code, there are really such OO ideas applied.

 
Reply With Quote
 
 
 
 
razael1@gmail.com
Guest
Posts: n/a
 
      11-15-2006
On Nov 15, 10:15 am, "Cong Wang" <(E-Mail Removed)> wrote:
> > ThierryYeah. This is an interesting topic. In fact, C can NOT fully support

> OOP like C++, but we can use some tricks to make it *like* an
> object-oriented programming language. For example, we can use
> structures to replace classes, use function pointer as a member of a
> structure like a method of a class. And in fact, in Linux kernel source
> code, there are really such OO ideas applied.


That's just one of the three requirements for an OO programming
language, encapsulation. The other two are inheritance and
polymorphism, and those are much more difficult to emulate in C. I've
seen inheritance attempted by exploiting the fact that C will let you
cast any type to any other type without complaining. So basically:

struct Parent
{
int a,b;
};

struct Child
{
int a,b;
more variables
};

struct Child2
{
int a,b;
more variables
}

If you want to pass a Child or a Child2 to a function, instead of
making two version of the function (which need different names because
there is no overloading in C), you can make the function take a Parent,
and then pass a Child or a Child2 'c' by saying (Parent)c. I'm not
sure if this just happens to work, or if it is actually defined in the
C standard that the variables of a structure appear in memory in the
order that they are specified in the declaration.

I haven't seen any way to do polymorphism. I guess that's kind of like
templates in C++, although templates are technically not polymorphism,
they are a hack to acheive the effect of polymorphism. This is because
the templates are handled at compile time, and true OO polymorphism is
runtime polymorphism.

Colin K.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-15-2006
(E-Mail Removed) wrote:
> On Nov 15, 10:15 am, "Cong Wang" <(E-Mail Removed)> wrote:
> > > ThierryYeah. This is an interesting topic. In fact, C can NOT fully support

> > OOP like C++, but we can use some tricks to make it *like* an
> > object-oriented programming language. For example, we can use
> > structures to replace classes, use function pointer as a member of a
> > structure like a method of a class. And in fact, in Linux kernel source
> > code, there are really such OO ideas applied.

>
> That's just one of the three requirements for an OO programming
> language, encapsulation. The other two are inheritance and
> polymorphism, and those are much more difficult to emulate in C. I've
> seen inheritance attempted by exploiting the fact that C will let you
> cast any type to any other type without complaining.

<snip>
> I haven't seen any way to do polymorphism...

<snip>

But the biggest gain of all in using a true OO enabled language like
C++ is the compiler support for OO constructs. C wasn't designed for
OOP and with increasing availability of C++ compilers for most
non-embedded platforms, it would be wiser to use the latter, (or
another OO language), unless there're strong reasons to favour C, (like
writing low-level system code, or desiring maximum portability etc).

 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      11-15-2006

"Thierry Chappuis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
>
> I'm interested in techniques used to program in an object-oriented way
> using the C ANSI language. I'm studying the GObject library and Laurent
> Deniau's OOPC framework published on his web site at
> http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is
> very instructive. I know that I could do much of this stuff with e.g.
> C++, but the intellectual challenge of implementing these concepts with
> pure ANSI C is relevant to me.
>
> Are you aware of another approaches? Any experience in using such
> techniques in production code? The use of GObject seems to be well
> implemented in the GNOME world, but I didn't find much about Laurent
> Deniau's OOPC. Have you some comments about the strengths and drawbacks
> of such techniques?
>
> Many thanks for your valuable help and comments
>
> Best regards
>
> Thierry
>


Look at the way a Widget is defined in the Unix Xt or Motif libraries.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-15-2006
Cong Wang wrote:

> Yeah. This is an interesting topic. In fact, C can NOT fully support
> OOP like C++,


It doesn't have syntactic support -- and that makes writing OO
code a real bigger -- but you can do polymorphism/inheritance
if you must; I think there's just enough room in the Standard
to make the required machinery legal.

Painful, but possible.

Who knows. Maybe someone could write a translator from an OO
language like C++ into pure C! Do you think that's possible?

--
Chris "Cleft? Cright? Cback? Cside? I do like to be ..." Dollin
Nit-picking is best done among friends.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-15-2006
Chris Dollin wrote:
> Cong Wang wrote:
>
> > Yeah. This is an interesting topic. In fact, C can NOT fully support
> > OOP like C++,

<snip>

> Who knows. Maybe someone could write a translator from an OO
> language like C++ into pure C! Do you think that's possible?


Didn't very early versions of g++ do that?

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-15-2006
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:

>That's just one of the three requirements for an OO programming
>language, encapsulation. The other two are inheritance and
>polymorphism


Requirements? Common characteristics perhaps.

>struct Parent
>{
> int a,b;
>};
>
>struct Child
>{
> int a,b;
> more variables
>};


>
>struct Child2
>{
> int a,b;
> more variables
>}
>
>If you want to pass a Child or a Child2 to a function, instead of
>making two version of the function (which need different names because
>there is no overloading in C), you can make the function take a Parent,
>and then pass a Child or a Child2 'c' by saying (Parent)c. I'm not
>sure if this just happens to work, or if it is actually defined in the
>C standard that the variables of a structure appear in memory in the
>order that they are specified in the declaration.


The structure specifies that given a union of two struct types with
common initial sequences, you can access that common part through
either of the union members. Since given independent compilation it
is often impossible to be certain whether a structure is in a union
with another suitable type, in practice this is guaranteed to work.

>I haven't seen any way to do polymorphism.


Structures containing pointers to functions. For example, give every
type that can draw itself a member void (*draw)(Type obj, Screen screen),
and call obj->draw(obj, screen). Not very elegant, but you can define
a wrapper.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-15-2006
In article <ejffdp$g3n$(E-Mail Removed)>, I wrote:

>The structure specifies that given a union of two struct types with

^^^^^^^^^

This should have read "standard" of course.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Jean-Marc Bourguet
Guest
Posts: n/a
 
      11-15-2006
"santosh" <(E-Mail Removed)> writes:

> Chris Dollin wrote:
> > Cong Wang wrote:
> >
> > > Yeah. This is an interesting topic. In fact, C can NOT fully support
> > > OOP like C++,

> <snip>
>
> > Who knows. Maybe someone could write a translator from an OO
> > language like C++ into pure C! Do you think that's possible?


At the very worse, one would code in C an emulator for a x86 and then run
the emulator on the generated X86 code which would be a very large constant
for the program. Obviously C can be coded at such a low level than there
are better approaches.

BTW, C code is quite common as a target langage for early compilers of new
langages.

> Didn't very early versions of g++ do that?


Not that I know of. CFront did that. Comeau still do it. I think EDG
provides an example back end for its front end which does it.

That's not very different that doing a compiler. What would be more
problematic is doing such a compiler which produce mainable C code.

Yours,

--
Jean-Marc
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pre-ansi to ansi c++ conversion? Frank Iannarilli C++ 2 07-21-2009 11:05 PM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C Programming 11 04-28-2008 03:00 AM
Are there statistics packages in ANSI C and/or ANSI C++? lbrtchx@gmail.com C++ 1 04-24-2008 06:44 PM
next ISO/ANSI standard for C++ skoco C++ 20 02-25-2004 03:10 PM
ISO/ANSI C++ Standard copy Chris Mantoulidis C++ 3 12-13-2003 03:37 PM



Advertisments