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?
February 14th, 2009 at 1:42 am
C++ without classes?!?!?!
That’s like like trying corn flakes without milk. Not really giving it much of a go are you?
February 14th, 2009 at 5:32 am
Bubba, why not? Classes are not the only good thing C++ has when compared to C (assuming that’s what you meant).
February 14th, 2009 at 7:11 am
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)
February 14th, 2009 at 9:52 am
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.
February 14th, 2009 at 12:39 pm
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.
February 14th, 2009 at 3:28 pm
What are some of the benefits of C++ over C in your use then?
February 17th, 2009 at 9:07 am
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.
February 17th, 2009 at 7:19 pm
“For my in-game objects, I just define a single struct that has all the possible variables I need.”
Use unions where possible.
February 19th, 2009 at 2:20 pm
>I’m using structs instead of classes
Matters not.
>Use unions where possible.
So out of context.
March 23rd, 2009 at 11:51 am
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.
April 25th, 2009 at 1:54 am
You switched from C to C++ because not having classes was too painful, and now you use structs instead of classes? 😉