how to use pure c to write OO programs

G

Greg Comeau

I don't see the point of using a C++ frontend (presumably something
that translates C++ to C) as opposed to a C compiler.

The C generated by a translator is unlikely to be maintainable or even
legible. In ordinary usage, you're never even going to look at it;
you'll just feed it to a C compiler. Assuming a C++ compiler is
available for your platform, there's no real benefit in generating
unmaintanable C as an intermediate step.

There are ways to do OO in C, but they tend to be clumsy compared to
using a language with OO facilities built-in.

Indeed, there is little point in just compiling to C.
Especially as usually that alone is insufficient.
 
G

Greg Comeau

Precisely. Which is why my first advice is: don't do it. The second is:
if you *must* get C for some reason (you need to interface directly with
existing C code, your compiler supports only C) and you *must* use an
O-O design (for reasons I can't make up, but there undoubtedly will be
some) why *not* a frontend?

Because there is another way to phrase the question:
Why not just use C++, of any form? As Keith pointed out
the generated C code is not going to be a new maintainable
source code. This is not to say there is never any benefit
to such an arrangementi (or I've led myself astray for 20 year :})
but the C code in and of itself is not a solution; the solution
is an integrated mechanism to object the desired and eventual
native object code. (Again, assuming this is what the OP
is after.)
 
G

Greg Comeau

Skarmander said:
Keith Thompson wrote: [...]
I don't see the point of using a C++ frontend (presumably something
that translates C++ to C) as opposed to a C compiler.

Because a C compiler won't let you compile C++. You meant "as opposed
to a C++ compiler", perhaps? Or just "write in C"?

Whoops, I meant "as opposed to a C++ compiler".

BTW, a C++ frontend (translating C++ to C) in combination with a C
compiler *is* a C++ compiler.

Then your statement is unclear, because it seems to be saying
"I don't see the point of using a C++ compiler as opposed to a
C++ compiler"
The fact that it uses C as an
intermediate language isn't likely to be particularly useful in any
likely scenario I can think of. You can examine the generated C code
to see what symbols it uses, but you can just as easily examine an
object file, which is likely to be about as legible.

If it's not particularly useful, then neither are other
intermediate forms. I think I'm not understanding the above.
 
K

Keith Thompson

Skarmander said:
Keith Thompson wrote: [...]
I don't see the point of using a C++ frontend (presumably something
that translates C++ to C) as opposed to a C compiler.

Because a C compiler won't let you compile C++. You meant "as opposed
to a C++ compiler", perhaps? Or just "write in C"?

Whoops, I meant "as opposed to a C++ compiler".

BTW, a C++ frontend (translating C++ to C) in combination with a C
compiler *is* a C++ compiler.

Then your statement is unclear, because it seems to be saying
"I don't see the point of using a C++ compiler as opposed to a
C++ compiler"

Yes, I was probably unclear.

A C++ compiler, regardless of what intermediate language it might use,
is a good tool for writing OO programs (assuming, for the sake of
argument, that C++ is a good language for OO programs).

If your goal, as the subject of this thread says, is to "use pure c to
write OO programs", a C++ compiler that produces C as an intermediate
is not likely to be particularly useful (unless the C++ compiler
somehow manages to generate maintainable C code). I can imagine it
might be useful in some circumstances: if the target machine has a C
compiler but no C++ compiler, and the C++/C translator generate
suffciently portable C code that it can be compiled for the target.

But since the original poster has never bothered to come back and
clarify what he's looking for, there's not much point in speculating.
 
G

Greg Comeau

[email protected] (Greg Comeau) said:
Keith Thompson wrote:
[...]
I don't see the point of using a C++ frontend (presumably something
that translates C++ to C) as opposed to a C compiler.

Because a C compiler won't let you compile C++. You meant "as opposed
to a C++ compiler", perhaps? Or just "write in C"?

Whoops, I meant "as opposed to a C++ compiler".

BTW, a C++ frontend (translating C++ to C) in combination with a C
compiler *is* a C++ compiler.

Then your statement is unclear, because it seems to be saying
"I don't see the point of using a C++ compiler as opposed to a
C++ compiler"

Yes, I was probably unclear.

A C++ compiler, regardless of what intermediate language it might use,
is a good tool for writing OO programs (assuming, for the sake of
argument, that C++ is a good language for OO programs).
Agreed.

If your goal, as the subject of this thread says, is to "use pure c to
write OO programs", a C++ compiler that produces C as an intermediate
is not likely to be particularly useful (unless the C++ compiler
somehow manages to generate maintainable C code). I can imagine it
might be useful in some circumstances: if the target machine has a C
compiler but no C++ compiler, and the C++/C translator generate
suffciently portable C code that it can be compiled for the target.

I thought you originally said something like that, and agreed. :)
Furthermore, you will really never get the generated code to be truly
the "new source code", though lots and lots of work can go into
such a thing, and of course that goal would not seem directly beneficial
especially since there are other choices.
But since the original poster has never bothered to come back and
clarify what he's looking for, there's not much point in speculating.

Still agreed. :)
 
Z

Zara

can some one tell that how to use pure c to write OO programs?

thanks in advanced!


OP has never reappeared, although there have been some nice debates
going on, and his/her presence has been requested...
will he/she be a stone-throwing troll?
 
A

Arctic Fidelity

OP has never reappeared, although there have been some nice debates
going on, and his/her presence has been requested...
will he/she be a stone-throwing troll?

It's possible. ;-) I do have one question though. Throughout this
discussion, I have seen a lot of talk about C++ and overhead. I'm just
curious about a scenario like this.

Say you are normally a C user (like me) but for some reason you may, for
one reason or another, require or desire the usage of real OOP facilities
in the language you are using. It seems that OOP in ANSI C is a bit iffy,
and C++ is rather "large" by what I gather (and I am rather
inexperienced), but, I have not heard anyone discuss Objective-C. It was
my understanding that Objective-C was more of a wrapper on C (using that
word loosely), and would better fit the requirements of a C user who
wanted to add OOP into the language, but perhaps not all the extra stuff
of C++. Am I wrong here?

I would appreciate any of you who could comment on this. I have sometimes
wondered if something I wanted might be better implemented in OOP, and I
have thought about Objective-C, but I have not really understood where
they all stand for the normal C user who wants OOP.

Thanks,

- Arctic Fidelity
 
K

Keith Thompson

Arctic Fidelity said:
It's possible. ;-) I do have one question though. Throughout this
discussion, I have seen a lot of talk about C++ and overhead. I'm just
curious about a scenario like this.

Say you are normally a C user (like me) but for some reason you may,
for one reason or another, require or desire the usage of real OOP
facilities in the language you are using. It seems that OOP in ANSI C
is a bit iffy, and C++ is rather "large" by what I gather (and I am
rather inexperienced), but, I have not heard anyone discuss
Objective-C.

We don't discuss Objective C here because it isn't C.

There is a comp.lang.objective-c newsgroup.
 
F

Flash Gordon

EventHelix.com said:
C++ overhead is not as high as it is generally assumed.

<snip>

Why tell us? We are interested in C here, not C++.

If it was in response to some previous post here, then provide context
since the post you are responding to may well not have reached everyone
your post reached.
 
W

werasm

Flash said:
<snip>

Why tell us? We are interested in C here, not C++.

Clearly, the post(er) he replied to was not only interested in C. He
therefore had good reason to mention this.
If it was in response to some previous post here, then provide context
since the post you are responding to may well not have reached everyone
your post reached.

Yes, he forgot to provide the previous context.

Werner
 
C

Christopher Benson-Manica

werasm said:
Clearly, the post(er) he replied to was not only interested in C. He
therefore had good reason to mention this.

The point is that comp.lang.c++ is available if people want to discuss
that language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top