Conquer the island
Slay
Board game
Conquest
Command a squadron of spaceships
Critical Mass
Board game of bluffing
The General
Conquer the world
Mother Of All Battles
Real time WWII battle
Firefight
Capture The Flag
Capture The Flag
A card game
Niggle
Lead your tribe to safety
End Of Atlantis
Play in the Jurassic League
Football-o-
saurus

Kill all of the rats
Rats!
Topple
Topple
Find all 35 words
Word
Storm


The Games
Slay
Conquest
Critical Mass
The General
Mother Of All Battles
Firefight
Capture The Flag
Niggle
End Of Atlantis
Football-o-saurus
Rats!
Foosball
UFOs
Topple
WordStorm

Info
arrowDownload free versions
arrowBuy full versions
arrowHistory of the games
arrowLink to this site
arrowThanks to...

Contact
arrowForums
arrowEmail me

About Sean O'Connor
arrowCV
arrowOther Projects
arrowPhotos

Convert Objective C to C++

I needed a way to convert my Objective C code for my iOS games into C++ so I could port them to Windows and Android. I couldn't find anything available to do it so I wrote a Windows app which will do most of the tedious work and preserve all my code comments.



Here's some Objective C
// imports
#import "header.h"

// defines
#define TEST_DEFINITION			10
#define TEST_MACRO(x)			(x+1)

@implementation TestClass

#pragma mark -
#pragma mark Init

-(id)init
{
	if (self = [super init])	// it doesn't handle this well so 'init's need to be fixed by hand
	{
		m_iMemberVariable = 0;
	}
	
	return (self);
}

-(BOOL)testMethod:(TestObject1*)pObject1 (TestObject2*)pObject2
{
	short int		iLocalVariable;
	
	// single line comment
	
	/*  multiple
		line
		comment
	*/
	
	pObject = [[Object alloc] init];
	[pObject release];
	iTestArray[64] = 3;	// mustn't be fooled into thinking the [ is the beginning of a method call!
	strcpy(szBuffer, "[don't be fooled by the [!");
	[self methodNoParamaters];	// comment on end of line
	[self methodWithOneParameter:8];
	[self methodWithMultipleParameters:8 and:10];
	[pObject methodWithMethodAsParameter:[pObject test]];
	[pObject methodWithMultiLineParameters:[pObject test1]
									and:[pObject test2:9]];
									
#ifdef CONDTIONAL_COMPILATION_TEST
	i = 3;
#endif

	for (iBracketTest=0; iBracketTest<1; iBracketTest++)
	{
		switch ([pObject method:test1 and:test2])
		{
			case 1: break;
			case 2: [pObject test]; break;
			default: return FALSE;
		}
	} 
	
	return TRUE;	
}

@end
						
						
And this is it automatically converted to C++:
// imports
#include "header.h"

// defines
#define TEST_DEFINITION			10
#define TEST_MACRO(x)			(x+1)



#pragma mark -
#pragma mark Init

TestClass::TestClass()
{
	if (self = __super::->init())	// it doesn't handle this well so 'init's need to be fixed by hand
	{
		m_iMemberVariable = 0;
	}
	
	return (self);
}

BOOL TestClass::testMethod(TestObject1* pObject1, TestObject2* pObject2)
{
	short int		iLocalVariable;
	
	// single line comment
	
	/*  multiple
		line
		comment
	*/

	
	pObject = new Object;
	pObject delete;
	iTestArray[64] = 3;	// mustn't be fooled into thinking the [ is the beginning of a method call!
	strcpy(szBuffer, "[don't be fooled by the [!");
	this->methodNoParamaters();	// comment on end of line
	this->methodWithOneParameter(8);
	this->methodWithMultipleParameters(8, 10);
	pObject->methodWithMethodAsParameter(pObject->test());
	pObject->methodWithMultiLineParameters(pObject->test1(), pObject->test2(9));
									
#ifdef CONDTIONAL_COMPILATION_TEST
	i = 3;
#endif

	for (iBracketTest=0; iBracketTest<1; iBracketTest++)
	{
		switch (pObject->method(test1, test2))
		{
			case 1: break;
			case 2: pObject->test(); break;
			default: return FALSE;
		}
	} 
	
	return TRUE;	
}						
						
What it doesn't do:
- It doesn't handle all the [super init] stuff and return 'self' in an init as C++ constructors are different.
- It doesn't convert any NS or CG functions because those are Apple functions.
- It doesn't do any of that crazy Objective C retain, auto release memory management stuff.
- It doesn't do properties, protocols, categories etc...
- Objective C doesn't mind you calling a method on a NULL object. C++ will crash if you do that.