Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-19-2010, 11:34 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 1
foslacocoa is on a distinguished road
Default Merging C++ code with objective c

Hi,

I want to know that whether C++ code can be merged with objective c or not ?
foslacocoa is offline   Reply With Quote
Old 04-20-2010, 09:13 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by foslacocoa View Post
Hi,

I want to know that whether C++ code can be merged with objective c or not ?
To some extent, yes, since C++ and Obj-C share the common ancestor, C. But there are many, many syntactical differences between the languages that would make "merging" difficult and time-consuming.

Xcode can build projects comprised of a mix of C++ and Obj-C source files, so you can use existing C++ code (or continue writing new C++ code) as long as you build an interface layer back to Obj-C wherever needed.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-21-2010, 03:38 AM   #3 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Mr Jack is on a distinguished road
Default

Or if you rename your .cpp files to .mm, they will compile as Obj-C++ and can call both C++ and Obj-C code directly. I suggest a thin layer of Obj-C++ files as interface between the two personally.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 04-22-2010, 12:13 AM   #4 (permalink)
Registered Member
 
mobileben's Avatar
 
Join Date: Jul 2009
Location: Zgrunturos
Posts: 161
mobileben is on a distinguished road
Default

It's relatively easy to intermix C++ and Obj-C. There are a few drawbacks due to syntax. For example if you were to do something like:

int myArray = new int[[objCClass length]];

The compiler doesn't like this. So you end up doing something like:

int count = [objCClass length];
int myArray = new int[count];

Our projects intermix the two, with a heavy bias towards C++. As Mr Jack stated, you can rename the file. The other method is to use Get Info on the files and change the file type to sourcecode.cpp.objcpp. This works for any code file type (like .m).

We prefer this method ourselves mainly because all our code also runs on the PC. So it allows us to use .cpp the majority of time and use .m when we have truly iPhone specific classes or files, like delegates.
__________________
Have a Poketastic time with QuitIt!
Arcade Basketball at it's best! Hoops Madness!
VS action on the iPad with Hoops Madness VS!
Sweet trailer here!
mobileben is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 460
16 members and 444 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, mer10, Mijator, mkjarred, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:29 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0