Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Having C code looking like C++ code

Reply
Thread Tools

Having C code looking like C++ code

 
 
Fronsac
Guest
Posts: n/a
 
      10-12-2003
Hi,
I've been asked in a job interview how to make C code look like C++
code, and honestly I didn't know what to answer because I have never really
done a lot of C. Now, I've been searching around the web about web sites
that talk about this subject, but I've had no luck. Can anyone point me to
some web site about this subject? Thanks a lot!


 
Reply With Quote
 
 
 
 
bd
Guest
Posts: n/a
 
      10-12-2003
Fronsac wrote:

> Hi,
> I've been asked in a job interview how to make C code look like C++
> code, and honestly I didn't know what to answer because I have never
> really done a lot of C. Now, I've been searching around the web about web
> sites that talk about this subject, but I've had no luck. Can anyone point
> me to some web site about this subject? Thanks a lot!


Making C code look like C++ generally requires making it actually be C++.
Why would you try to make one language only look like another? There's no
advantage, unless you're trying for the IOCCC.
 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      10-12-2003
In article <AL1ib.48789$(E-Mail Removed)>,
Fronsac <(E-Mail Removed)> wrote:
>Hi,
> I've been asked in a job interview how to make C code look like C++
>code, and honestly I didn't know what to answer because I have never really
>done a lot of C. Now, I've been searching around the web about web sites
>that talk about this subject, but I've had no luck. Can anyone point me to
>some web site about this subject? Thanks a lot!


Answer #1: Why would you want to?
Answer #2: By writing bad C code
Answer #2b: By writing C code that looks like bad C++ code
Answer #3: By writing in the common subset of C and C++ (but see #1 and #2)
Answer #4: What are you really trying to find out?

One possibility is that they want C++-clean C code for a possible upgrade
path to C++. The correct solution to this is to use C++'s `extern "C"'
to allow C and C++ code to be linked together, and have C (compiled as
C) and C++ (compiled as C++) linked together when they want old (C)
code and new (C++) code in the same program. All that this requires
is that the header files used be both C-clean and C++-clean, which is
rather easier than the general case of all code.


Unless they're talking about all the angle brackets that C++ uses.
You might be able to get away with using the <: and :> digraphs for that.


dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
>[S]till not good enough for Richard Heathfield, but what can you do, really?

You could always write "only for clever people" on the cover.
--Ben Pfaff and Richard Heathfield in comp.lang.c
 
Reply With Quote
 
Fronsac
Guest
Posts: n/a
 
      10-12-2003
> Making C code look like C++ generally requires making it actually be C++.
> Why would you try to make one language only look like another? There's no
> advantage, unless you're trying for the IOCCC.


C++ is not always accessible on some platforms (think game consoles like the
old game boy might not have access to a C++ compiler), so you'll have to use
C and some tricks to have it behave like C++. From what I've gathered so
far, it's impossible with C syntax to achieve inheritence, though you can
fake it. For example :

struct Foo {
int var1;
}

struct Bar {
struct Foo base;
int var2;
}

Bar* createBar() {
return malloc( sizeof(Bar) );
}

void foo_incVar1( Foo* foo ) {
foo->var1++;
}

void bar_incVar2( Bar* bar ) {
bar->var2++;
/* though, you realize that if I want to access var1, I have to do
bar->base.var1, or ((Foo*)bar)->var1).
The thing that bugs me most though is that you can encapsulate your
data. */
}

void main() {
Foo* foo = (Foo*) createBar();
foo_incVar1( foo ); /* increments var1 */
bar_incVar2( (Bar*) foo ); /* increments var2 */
}

And you can use function pointers to simulate virtual methods for your
"objects".


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-12-2003
"Fronsac" <(E-Mail Removed)> wrote:

>Hi,
> I've been asked in a job interview how to make C code look like C++
>code, and honestly I didn't know what to answer because I have never really
>done a lot of C. Now, I've been searching around the web about web sites
>that talk about this subject, but I've had no luck. Can anyone point me to
>some web site about this subject? Thanks a lot!


You posted the same question with slightly different subject lines
to (at least) a.c.l.l.c-c++ and c.l.c.

If you feel the need to post to more than one news-group, please
cross-post rather than multi-post. Thank you.

BTW: C++ is off-topic in c.l.c.

Irrwahn,
change codepage to make english text look like chinese text.
--
Three is no spimle sbtsuuttie for cearful, cercrot,
wtlirtew-len Esglinh. Tehre is no slveir beullt.

(Rhacrid Hfiaehlted in c.p, 2003-09-25)
 
Reply With Quote
 
Nathan
Guest
Posts: n/a
 
      10-12-2003
> Hi,
> I've been asked in a job interview how to make C code look like C++
> code, and honestly I didn't know what to answer because I have never

really
> done a lot of C. Now, I've been searching around the web about web sites
> that talk about this subject, but I've had no luck. Can anyone point me to
> some web site about this subject? Thanks a lot!


Hi,

Do you mean you want to do object oriented programming in C? In that case,
there are many ways of doing this. The following article describes one way:
http://www.accu.org/acornsig/public/articles/oop_c.html

Regards,
Nathan


 
Reply With Quote
 
Fronsac
Guest
Posts: n/a
 
      10-12-2003
> You posted the same question with slightly different subject lines
> to (at least) a.c.l.l.c-c++ and c.l.c.


Yeah, I was too lazy to copy-paste the subject line from the other post. I
posted in two forums just to be sure at least one person answered me.

> If you feel the need to post to more than one news-group, please
> cross-post rather than multi-post. Thank you.


What is cross-post? Could you explain me what it is and how to do it? Thanks
a lot.

> BTW: C++ is off-topic in c.l.c.


That's fine. I only asked here because I wanted to know more about a certain
way to code in C, and I thought here would be a great place to start.



 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-12-2003
Fronsac <(E-Mail Removed)> scribbled the following:
>> If you feel the need to post to more than one news-group, please
>> cross-post rather than multi-post. Thank you.


> What is cross-post? Could you explain me what it is and how to do it? Thanks
> a lot.


Cross-posting is posting the same article to multiple newsgroups
simulatenously. Not identical copies of the article - the SAME article.
How to actually do this depends on your newsreader. All that is NEEDED
is that the NNTP headers have a "Newsgroups:" line consisting of
multiple newsgroup names (separated with commas), but not all
newsreaders allow you to edit NNTP headers directly. (tin, which I use
myself, does.)
Another way, and one recommended for Usenet beginners who don't yet
know how NNTP works, is to look around the "compose message" (or
similar) window and search for a text box labelled "Newsgroup" or
something. Then just type multiple group names (separated with commas)
there. Depending on your newsreader, it might or might not work. It
will work on most non-brain-dead newsreaders. (I count Microsoft
Outlook Express as non-brain-dead, but Google as brain-dead.)

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You could take his life and..."
- Mirja Tolsa
 
Reply With Quote
 
stelios xanthakis
Guest
Posts: n/a
 
      10-12-2003
"Nathan" <(E-Mail Removed)> wrote in message news:<bmbfos$c1n$(E-Mail Removed)>...
> > Hi,
> > I've been asked in a job interview how to make C code look like C++
> > code, and honestly I didn't know what to answer because I have never

> really
> > done a lot of C. Now, I've been searching around the web about web sites
> > that talk about this subject, but I've had no luck. Can anyone point me to
> > some web site about this subject? Thanks a lot!

>
> Hi,
>
> Do you mean you want to do object oriented programming in C? In that case,
> there are many ways of doing this. The following article describes one way:
> http://www.accu.org/acornsig/public/articles/oop_c.html
>


Or you can study the lightweight C++ preprocessor, which takes as input
a language that looks like C++ and produces good old C.

http://students.ceid.upatras.gr/~sxanth/lwc/index.html

Next version has template specializations and other goodies.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      10-12-2003
"Fronsac" <(E-Mail Removed)> writes:

> I've been asked in a job interview how to make C code look like C++
> code, and honestly I didn't know what to answer because I have never really
> done a lot of C. Now, I've been searching around the web about web sites
> that talk about this subject, but I've had no luck. Can anyone point me to
> some web site about this subject? Thanks a lot!


You could name all of your header files without .h suffixes.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
 
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
Which film characters would you least like having as a neighbor and why ? Eric DVD Video 3 09-14-2007 12:26 PM
Internet networking , group networking,business leads,new business opportunities,like having many sales peoples elie Computer Support 0 08-18-2007 10:12 AM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Would like to load a datagrid already in edit mode instead of having the user click the edit button Frank Kurka ASP .Net Datagrid Control 8 04-29-2005 09:33 AM
After having 8mm film reels digitally archived, film looks very grainy/ filled with static. Is this digital-looking noise normal? + more 8mm film questions Phil Edry Digital Photography 11 10-10-2004 11:57 PM



Advertisments