Adv or Disadv of Object oriented programming in C instead of C++ ?????

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Deepak Gandotr » Sun, 28 May 2000 04:00:00



Does anyone have an Idea as to how to implement object oriented design
in language C ?

Although this may lead to a little loss of performance in terms of
coding and execution but I have seen many of the c++ compilers generate
the c code from c++ code and then compile it.

Is there any standard for this. If yes what is it ?

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by John Harriso » Sun, 28 May 2000 04:00:00



Quote:> Does anyone have an Idea as to how to implement object oriented design
> in language C ?

> Although this may lead to a little loss of performance in terms of
> coding and execution but I have seen many of the c++ compilers generate
> the c code from c++ code and then compile it.

> Is there any standard for this. If yes what is it ?

> Sent via Deja.com http://www.deja.com/
> Before you buy.

I don't think you'll find any performance loss at all, what you will find is
that the coding is a lot more difficult because you have to remember to
adopt a certain style and do things manually that C++ would do
automatically. Among the difficulties I can think of are

a) It's not possible to overide the assignment operator, you'll have to
define a function and remember to use it instead of =.

b) It's not possible to define a destructor, you'll have to define a
function and remeber to call it every time variable goes out of scope.

c) It's not possible to define a constructor, you'll have to define a
function and remeber to call it when a you declare a variable.

d) It's not possible to define a copy constructor, you'll have to define a
function and remeber to call it when a you initialise of variable with
another. This includes when you pass an object as function parameter.

e) It's not possible to do inheritance cleanly, you'll have to declare the
base class inside the derived class, and handle the pointer casting
yourself.

f) There no way of decaring data members private, you'll just have to
remember not to use them.

g) It not possible do to exceptions in C. You can do the non local transfer
of control with setjmp/longjmp but stack unwinding is impossible.

I'm sure there are other difficulties too. Basically you're going to have to
adopt an extremely restricted programming style (for instance, allocate all
objects on the heap and pass them around as pointers, this gets round
objections a, b, c and d) or you going to have to do a lot of fiddly
programming which will be prone to bugs.

john

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by John Harriso » Sun, 28 May 2000 04:00:00



Quote:> Does anyone have an Idea as to how to implement object oriented design
> in language C ?

> Although this may lead to a little loss of performance in terms of
> coding and execution but I have seen many of the c++ compilers generate
> the c code from c++ code and then compile it.

> Is there any standard for this. If yes what is it ?

> Sent via Deja.com http://www.deja.com/
> Before you buy.

I don't think you'll find any performance loss at all, what you will find is
that the coding is a lot more difficult because you have to remember to
adopt a certain style and do things manually that C++ would do
automatically. Among the difficulties I can think of are

a) It's not possible to overide the assignment operator, you'll have to
define a function and remember to use it instead of =.

b) It's not possible to define a destructor, you'll have to define a
function and remeber to call it every time variable goes out of scope.

c) It's not possible to define a constructor, you'll have to define a
function and remeber to call it when a you declare a variable.

d) It's not possible to define a copy constructor, you'll have to define a
function and remeber to call it when a you initialise of variable with
another. This includes when you pass an object as function parameter.

e) It's not possible to do inheritance cleanly, you'll have to declare the
base class inside the derived class, and handle the pointer casting
yourself.

f) There no way of decaring data members private, you'll just have to
remember not to use them.

g) It not possible do to exceptions in C. You can do the non local transfer
of control with setjmp/longjmp but stack unwinding is impossible.

I'm sure there are other difficulties too. Basically you're going to have to
adopt an extremely restricted programming style (for instance, allocate all
objects on the heap and pass them around as pointers, this gets round
objections a, b, c and d) or you going to have to do a lot of fiddly
programming which will be prone to bugs.

john

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Greg Come » Tue, 30 May 2000 04:00:00




>X-No-Archive: Yes

>> Although this may lead to a little loss of performance in terms of
>> coding and execution

Maybe, maybe not.

Quote:>>but I have seen many of the c++ compilers generate
>> the c code from c++ code and then compile it.
>Cfront did it. I'm not aware of any other compilers of this kind.

Comeau C++ does it.

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by John Harriso » Tue, 30 May 2000 04:00:00





> >X-No-Archive: Yes

> >> Although this may lead to a little loss of performance in terms of
> >> coding and execution

> Maybe, maybe not.

> >>but I have seen many of the c++ compilers generate
> >> the c code from c++ code and then compile it.
> >Cfront did it. I'm not aware of any other compilers of this kind.

> Comeau C++ does it.

> - Greg
> --
> Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
> Try Comeau C++ online at http://www.comeaucomputing.com/tryitout


How does it handle exceptions then?

john

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Greg Come » Wed, 31 May 2000 04:00:00








>> >X-No-Archive: Yes

>> >> Although this may lead to a little loss of performance in terms of
>> >> coding and execution

>> Maybe, maybe not.

>> >>but I have seen many of the c++ compilers generate
>> >> the c code from c++ code and then compile it.
>> >Cfront did it. I'm not aware of any other compilers of this kind.

>> Comeau C++ does it.

>How does it handle exceptions then?

Through a combo of generated code and compiler helper functions.

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by John Harriso » Thu, 01 Jun 2000 04:00:00









> >> >X-No-Archive: Yes

> >> >> Although this may lead to a little loss of performance in terms of
> >> >> coding and execution

> >> Maybe, maybe not.

> >> >>but I have seen many of the c++ compilers generate
> >> >> the c code from c++ code and then compile it.
> >> >Cfront did it. I'm not aware of any other compilers of this kind.

> >> Comeau C++ does it.

> >How does it handle exceptions then?

> Through a combo of generated code and compiler helper functions.

> - Greg
> --
> Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
> Try Comeau C++ online at http://www.comeaucomputing.com/tryitout


Could you give a short example? Its the stack unwinding part I'm interested
in.

I have thought (maybe I'm wrong) that you'd have to walk the stack to
destroy objects when a exception is thrown. I don't see how to do that in C
but I'd be very interested to learn.

john

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Jason Johnso » Wed, 07 Jun 2000 04:00:00



>g) It not possible do to exceptions in C. You can do the non local transfer
>of control with setjmp/longjmp but stack unwinding is impossible.

longjmp *does* unwind the stack.  I would not be surprised to find out that
is how c++ exceptions are done, or at least how they were implimented in
the beggining.

As far as the original question:  Yes is it definentally possible to do OO
programming in C.  Object Oriented-ness is a programmer attribute not
a language attribute.  A language *supports* OO programing to a certain
extent, or not at all.  For examples of OO programming in C look at the
linux kernel.

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Ron Natali » Thu, 08 Jun 2000 04:00:00




> >g) It not possible do to exceptions in C. You can do the non local transfer
> >of control with setjmp/longjmp but stack unwinding is impossible.

> longjmp *does* unwind the stack.  I would not be surprised to find out that
> is how c++ exceptions are done, or at least how they were implimented in
> the beggining.

Depends on the implementation.  In simpler architectures all it does is reload the
stack pointer and other registers that are not volatile across subroutine calls and
jmps to the appropriate location.

In cases where it does have to examine stack frames on it's way back up, it doesn't
do what C++ refers to as stack unwinding in that it doesn't call the C++ destruction
mechanism.  There is no good way to do this by CALLING setjmp/jongjmp.  Yes if you
were looking at the implementation of longjmp's stack unwinding, it probably would
be clear how it could be modified to make work.

Of course, then you're back to implementing one of the trickier parts of the C++
compiler implementation and you might as well let the compiler guys do it.

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Jason Johnso » Thu, 08 Jun 2000 04:00:00



<SNIP>

>Depends on the implementation.  In simpler architectures all it does is
reload the
>stack pointer and other registers that are not volatile across subroutine
calls and
>jmps to the appropriate location.

This is what I ment by "unwinding the stack".  By reloading the stack
pointer you
have "blown away" all the C local variables for all the functions that
happened
between the call to setjmp and longjmp.  Of course if one of those local
variables
was holding a resource like, for instance, a descriptor to a file opened in
one of those
functions.......

Quote:

>In cases where it does have to examine stack frames on it's way back up, it
doesn't
>do what C++ refers to as stack unwinding in that it doesn't call the C++
destruction
>mechanism.

Ok.  My mistake.  I didn't think about the fact that C++ is calling all the
local variable
desctructors.  The only times I have heard of "stack unwinding" in texts,
they have
been talking about the C behavior.
 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Greg Come » Thu, 08 Jun 2000 04:00:00




Quote:>I think whatever is possible in C++ is also
>possible in C (although with an awful amount of extra work.)

I think that saying such is a very delicate statement.
For instance, I can get from New York to California via car
or plane, so in that sense, both are able to accomplish
the same thing, but I think we want to stay away from
general statements that cars and planes are similar or can
accomplish the same things even though there are similarities.

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout

 
 
 

Adv or Disadv of Object oriented programming in C instead of C++ ?????

Post by Ye Olde Code » Fri, 09 Jun 2000 04:00:00




Quote:> Does anyone have an Idea as to how to implement object oriented design
> in language C ?

> Although this may lead to a little loss of performance in terms of
> coding and execution but I have seen many of the c++ compilers
generate
> the c code from c++ code and then compile it.

> Is there any standard for this. If yes what is it ?

I did this extensively in a situation where we had only a C compiler
and tools for an embedded DSP project.  I was programming analysis
tools in Java at the time, and was able to fairly well do certain
aspects of Java-'inspired' OO using a set of macros that mechanized
single linear inheritance.  The code looked something like:

class(FunctionGen)
{
   extends(AbstractFunctionGen);
   chain_delete();  /* 'virtual' destructor */
   float (*getNextVal)(AbstractFunctionGen*); /* member function */
   float someX;
   float someY;

Quote:};

static float getNextVal(AbstractFunctionGen*pSuper)
{
  LI_THIS(); /* extracts hidden pointer to this */
  super.val = this.someX*this.someY;
  this.someX *= 2.0;

Quote:}

CTOR(FunctionGen)
{
   SUPER_CTOR(AbstractFunctionGen);
   override(AbstractFunctionGen,getNextVal);
   this.someX = 0.0;
   this.someY = 0.0;

Quote:}

FuctionGen* a = new(FunctionGen); /* automatically invoked CTOR! */

and so on.  All with naming tricks and macros.  The key is exploiting
the fact that a pointer to a struct is also a pointer to the first
element of the struct to mechanize the principle that a pointer to a
derived class can be used anywhere a pointer to a base class is
expected (the LSP).  There was a lot more to it than the simple example
above.

I'm sure others have done similar things.  I just wanted to be clear on
my experience when I say:

DON'T DO THIS!!  The only advantage is if you absolutely, positively
cannot get a C++ compiler for your target processor.  The end product
using C/OO is slower, takes more dynamic memory, lot's more work, and
is far (far,far,far) less flexible than using C++.  Note that doing OO
yourself in C isn't related to the fact that some C++ compilers output
C.  The closer you try to match C++ (like, mechanizing VTables to avoid
a function pointer in each instance) the work just gets harder and
harder.

DON'T DO IT.

If you have a C++ compiler available, learn C++ and use that instead of
C (that's true beyond pure OO, C++ templates let you have your cake
(organized, layered design) and eat it too (throughput efficient)).

--
/\----|---------|
||  __o_________o_____
||  | Ye Olde Coder  |
||  VVVVVVVVVVVVVVVVVV

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

1. UCLA short course on "Object-Oriented Programming and Design Under the C++ Object Model"

On September 18-21, 1995, UCLA Extension will present the short course,
"Object-Oriented Programming and Design Under the C++ Object Model",
on the UCLA campus in Los Angeles.

The instructor is Mr. Stanley B. Lippman, Principal Software Engineer,
Walt Disney Feature Animation.

Each participant receives the text, "A C++ Primer", Second Edition, by
Stanley B. Lippman, and extensive course notes.

This course provides in-depth coverage of the entire C++ programming
language, including the latest extensions and modifications voted on by
the ANSI/ISO C++ committee, such as Run-Time Type Identification, Dynamic
Casts, and Namespaces.  Appropriate and inappropriate uses of C++
language features are illustrated.  In particular, the course focuses on C++'s
support for object-oriented programming in its inheritance mechanism,
methods of information hiding, and its strongly typed polymorphism.
Participants "walk through" detailed examples of virtual functions (dynamic
message binding), and virtual base classes under multiple inheritance.

The course also focuses on the issue of object-oriented design under a
multi-paradigm language such as C++.  Participants examine both the
concepts of object-oriented design and when the application of those
concepts do or don't make sense.  In particular, the course identifies the
many pitfalls inherent in real-world design.  The material is driven by an
extended interactive case study: adding a template facility to an existing
C++ implementation.  In the process, participants should gain a deeper
understanding of the C++ template facility.

Finally, the course presents an overview of the general C++ Object Model
(i.e., the representation of an object and object-oriented facilities implicit in
the language) in terms of program transformations and performance.  The
goal is to provide a better understanding of what goes on "under the covers"
so that participants can make informed implementation and design decisions,
and be spared unpleasant surprises.

The fee for the course is $1295, which includes the text and course materials.

For more information and a complete course description, please contact
Marcus Hennessy at:

(310) 825-1047
(310) 206-2815  fax

2. SuSE Linux 7.3 config

3. Palm III sighting at Staples

4. Standart C++ computer language for Object Oriented Programming ??

5. How much to refill each chamber in 3 color cartridge?

6. Live satellite videoconference on Object-Oriented programming and Software Reuse with C++

7. Teaching C++/Object Oriented Design, Analysis, Programming

8. Seminar in software reuse, C++ and object-oriented programming