Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Porting from C to C++ (http://www.velocityreviews.com/forums/t454137-porting-from-c-to-c.html)

rockstar 05-18-2006 08:41 PM

Porting from C to C++
 
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


Victor Bazarov 05-18-2006 08:56 PM

Re: Porting from C to C++
 
rockstar wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Jonathan Mcdougall 05-18-2006 08:57 PM

Re: Porting from C to C++
 
rockstar wrote:
> 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


Ian Collins 05-18-2006 09:45 PM

Re: Porting from C to C++
 
Jonathan Mcdougall wrote:
> rockstar wrote:
>
>> 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).


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 :)

--
Ian Collins.

rockstar 05-18-2006 11:25 PM

Re: Porting from C to C++
 
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


Ian Collins 05-18-2006 11:36 PM

Re: Porting from C to C++
 
rockstar wrote:
> 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.

--
Ian Collins.


All times are GMT. The time now is 12:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.