Phil Hassey - game dev blog
Phil Hassey as Snidely Whiplash
"You can't buy awesomeness.
You're born that way."

From python to C++

I think I wrote an article a few months ago comparing C and python.  I’m doing some new iPhone games now, and this time I’m using C++.  I found not having classes was getting to be too painful for me, so I’m trying out C++.  Here are a few things I’ve observed:

(As a side note – I’m writing my games /almost/ the same way I’d write them in python.  So if you’ve seen my game code, I’m doing almost the same stuff, except in C++.)

  • As usual, not having memory management is a bit of bother.  Fortunately, I’ve found there are only a few places where I actually need it.  The main thing I had to do was write some basic reference counting code for my state engine class.
  • I’m using structs instead of classes, since I have no use for private variables or methods.
  • Having separate .h and .cpp files is a drag.  But them’s the breaks .. It sure would be swell if C++ was a wee bit smarter about that.
  • For my in-game objects, I just define a single struct that has all the possible variables I need.  This is pretty much how I did things in python anyways.  I’ve never been a big fan of using much inheritance.
  • For writing my GUI classes, in python I was able to get lots of magic into them.  In C++ it isn’t so easy to get magic, so I’m doing without most of it.  It seems that most of the magic was gold-plating anyways.
  • Having written all this code in python before, getting clean C++ code seems pretty natural.  I know what I WANT my code to look like, so I try to get my C++ to be as close as possible to that. 
I think the bottom line of this post, is that my code is coming out pretty pythonic.  Maybe?

11 Responses to “From python to C++”

  1. Bubba Says:

    C++ without classes?!?!?!

    That’s like like trying corn flakes without milk. Not really giving it much of a go are you?

  2. Raul Pedro Santos Says:

    Bubba, why not? Classes are not the only good thing C++ has when compared to C (assuming that’s what you meant).

  3. Tony Arkles Says:

    Any particular reason you didn’t go with Objective-C? I’m just getting spun up on doing some iPhone hacking, and Objective-C looks pretty nice, compared to the crap I’m used to dealing with in the C++ world. (Although my day job involves writing Python, so I still feel like I’m taking a step back :D)

  4. philhassey Says:

    I’m using structs, which are classes except everything is public.

    As for ObjC, I want my games playable under Win/PC/OSX/consoles .. so using a language that has marginal support everywhere except for OSX/iPhone isn’t the best choice for me.

  5. Denilson Says:

    Your post basically sums up the idea that “learning one language improves your skills in another one”. I agree with that, as I notice it when I’m programming.

  6. Richard Jones Says:

    What are some of the benefits of C++ over C in your use then?

  7. philhassey Says:

    richard – I find using C++ structs for my objects much easier than doing the same with C structs. Methods and single inheritance and all that really cut out a lot of extra busy work.

  8. Michael Weller Says:

    “For my in-game objects, I just define a single struct that has all the possible variables I need.”

    Use unions where possible.

  9. Bojan Says:

    >I’m using structs instead of classes

    Matters not.

    >Use unions where possible.
    So out of context.

  10. Anonymous Says:

    Check out boost’s smart pointers if you want more python-like memory management in c++.

    c++ without boost is like python with its standard libraries.
    http://www.boost.org/

    Boost is HUGE, but you only link what you need.

  11. Chuckk Says:

    You switched from C to C++ because not having classes was too painful, and now you use structs instead of classes? 😉