Porting from C to C++

R

rockstar

Hi all,

I'm one of those wiz kids who knows a bit about all the main
programming languages (C, C++, Java, PHP, Python, etc...) but none of
them super good. I'm actually working as a software developer now, and
am working with PHP, skills are much improved, and I'm going to school
and learning Java, and doing C++ out of a bunch of books, mostly "Teach
Yourself C++ in 24 Hours" which seems to be helping a lot. I've hopped
on a few open source projects so that I can get exposure with the
language and learn the problem solving by actually doing it.

One of the first things I need to do is port a C program (actually a
pretty simple, single program) to C++. As I look at the code, I
understand what's going on. My biggest question is "What are some of
the biggest differences between C and C++" I'm talking syntax, etc.
I'm gonna have to port this whole program soon, and would like to get
the best C++ guys to warn me about pitfalls, etc. I'm using linux and
g++. Apparently, g++ compiles c as well so I figure I'll use that for
compilation.

Your input is appreciated, especially because I'd like to become
strong with C++.

Paul
http://eventuallyanyway.com
 
V

Victor Bazarov

rockstar said:
I'm one of those wiz kids who knows a bit about all the main
programming languages (C, C++, Java, PHP, Python, etc...) but none of
them super good.

I am guessing you meant "none of them well enough."
I'm actually working as a software developer now,
and am working with PHP, skills are much improved, and I'm going to
school and learning Java, and doing C++ out of a bunch of books,
mostly "Teach Yourself C++ in 24 Hours" which seems to be helping a
lot.

Check out www.accu.org (their book review section) to see what other
books are recommended.
I've hopped on a few open source projects so that I can get
exposure with the language and learn the problem solving by actually
doing it.

Not a bad idea.
One of the first things I need to do is port a C program (actually a
pretty simple, single program) to C++. As I look at the code, I
understand what's going on. My biggest question is "What are some of
the biggest differences between C and C++" I'm talking syntax, etc.

Have you tried looking on the web or in the news archives for that?
I'm gonna have to port this whole program soon, and would like to get
the best C++ guys to warn me about pitfalls, etc. I'm using linux and
g++. Apparently, g++ compiles c as well so I figure I'll use that for
compilation.

Pitfalls? Not too many. The simplest way to start is to rename your
files from .c to .C (or to .cxx, to .cc, to .cpp, etc.) to differentiate
them from what they were, and to compile them using a C++ compiler. If
it compiles, link, if links, run. If it does what you need, you've done
what you needed to do. If it breaks somewhere, figure out why and fix
it accordingly. That's all.

V
 
J

Jonathan Mcdougall

rockstar said:
One of the first things I need to do is port a C program (actually a
pretty simple, single program) to C++. As I look at the code, I
understand what's going on. My biggest question is "What are some of
the biggest differences between C and C++" I'm talking syntax, etc.
I'm gonna have to port this whole program soon, and would like to get
the best C++ guys to warn me about pitfalls, etc. I'm using linux and
g++. Apparently, g++ compiles c as well so I figure I'll use that for
compilation.

Don't let syntax fool you: C and C++ are two different languages. Even
if two languages use the same vocabulary, idioms may be completly
different.

"Porting" a C program to C++ is an easy task if you only want to
compile it (well written C code compiles on a C++ compiler, unless C++
keywords are used). However, if you want to use design patterns, idoms
and object-oriented features, you pretty much have to start from
scratch.

Converting "printf" to "std::cout" is not all there is to do!


Jonathan
 
I

Ian Collins

Jonathan said:
Don't let syntax fool you: C and C++ are two different languages. Even
if two languages use the same vocabulary, idioms may be completly
different.

"Porting" a C program to C++ is an easy task if you only want to
compile it (well written C code compiles on a C++ compiler, unless C++
keywords are used).

Not always, there a quite a few C idioms that won't compile as C++, a
couple of common examples:

Implicit cast from void*.
Assignments of integer types to enums.
Macros that use any of the above :)
 
R

rockstar

Yea, I've already encounted the void problem I believe, and have been
googling around to find the solution/workaround/port to get this stuff
to work. It also means I have to read the documentation for libraries
I'm not completely familiar with. Victor, as a rule of thumb, I always
google before asking questions, but I've already gotten some good
feedback to the whole process

Paul
http://www.eventuallyanyway.com
 
I

Ian Collins

rockstar said:
Yea, I've already encounted the void problem I believe, and have been
googling around to find the solution/workaround/port to get this stuff
to work. It also means I have to read the documentation for libraries
I'm not completely familiar with. Victor, as a rule of thumb, I always
google before asking questions, but I've already gotten some good
feedback to the whole process
Have a look at <http://cfaj.freeshell.org/google/> for some good advice
on posting followups.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top