Porting from C to C++

Discussion in 'C++' started by rockstar, May 18, 2006.

  1. rockstar

    rockstar Guest

    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
    rockstar, May 18, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, May 18, 2006
    #2
    1. Advertising

  3. 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
    Jonathan Mcdougall, May 18, 2006
    #3
  4. rockstar

    Ian Collins Guest

    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.
    Ian Collins, May 18, 2006
    #4
  5. rockstar

    rockstar Guest

    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
    rockstar, May 19, 2006
    #5
  6. rockstar

    Ian Collins Guest

    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.
    Ian Collins, May 19, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Overworked

    HELP - Porting to .NET path?

    Overworked, Jan 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    379
    Chad Z. Hower aka Kudzu
    Jan 23, 2004
  2. Nate
    Replies:
    2
    Views:
    413
    Yan-Hong Huang[MSFT]
    Feb 18, 2004
  3. DevBoy
    Replies:
    0
    Views:
    332
    DevBoy
    May 6, 2004
  4. Bijoy Naick

    Porting over an ASP.NET application

    Bijoy Naick, May 6, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    414
    Curt_C [MVP]
    May 10, 2004
  5. Oriane
    Replies:
    0
    Views:
    329
    Oriane
    Jul 6, 2004
Loading...

Share This Page