Objective-C++ for Objective-C Programmers

Hello everybody,

I have tried for two years to stay away from C++ but I think I'm going to give it ago! I have been programming in C/Objective-C/Cocoa for a few years but have never played with C++/Objective-C++ I was just wondering if anybody else is in the same boat?

Has anybody written a introduction to Objective-C++ from the point of few of an Objective-C programmer?

Dan.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Objective-C++ for Objective-C programmers

Using the Objective-C++ bridge is very straightforward once you know C++. Apple's half-page documentation here should be enough. I don't know of any 'C++ for the Objective-C programmer' resources, but here's a short paragraph from Wikipedia: link(check the "Philosophical Differences between Objective-C and C++" subsection). A good and free C++ book is Bruce Eckel's Thinking in C++, Volume I:link. The fundamental difference between C++ and Obj-C is the typing used. C++ is statically typed(compile-time typing), like C, and Obj-C is dynamically typed(runtime typing). In general, when I experiment with Objective-C++ I try to keep it simple. For example, I usually have some C++ classes I need to use in a Cocoa application. I create a C++ wrapper object that acts as the interface between the ObjC class and the other C++ classes. So I might have the following setup. This is contrived example, sorry :) .

//Obj-C
@interface FooBar: NSObject {
CPPWrapper *wrap; //this is the pointer to the C++ wrapper object
}

-(void) init;
-(void) doSomething;
@end

//C++
//declaration of CPPWrapper analogous to @interface
class CPPWrapper {
CPPWrapper(int x, int y, int z);
int computesum();
};

//Omitted C++ class definition(analogous to @implementation) for now.

//ObjC
@implementation FooBar
-(void) init {
wrap = new CPPWrapper(2,3,5);
wrap->computesum(); //analogous to [wrap computesum] if
//wrap was a pointer to an ObjC object
//and computesum was an ObjC method.
}

Couple of other things: to translate between Objective-C data types and C++ data types look at their common ancestor: C. So, ints, floats, doubles can be passed between ObjC objects and C++ objects very easily, but what about things like strings? It turns out C++ strings can be converted to C strings using the c_str() instance method, and NSStrings and NSMutableStrings can be converted to C strings using cStringUsingEncoding: There are some caveats with using Objective-C++ and Apple's documentation above details them.

Hope this helps,

Chinmoy

P.S. A long example of Objective-C++ is here
Another great C++ tutorial is here

My article on C++ compared to ObjC

Hi all,

I have been thinking about organizing my thoughts on C++ vs. ObjC for my own enrichment, and after reading this topic, I decided to go ahead and write it. You can find it here(link to part III, part I and part II also available at the same website), if you are interested. Please let me know of any inaccuracies in the code or concepts.

Regards,
Chinmoy.

Update: Part II has been written.
Update2: Part III has been written(not a lot of stuff, but a good background on smart pointers).

I'll be adding updates to this same post(you can also subscribe to the RSS feed on my blog if you want(look at the right hand pane for Entries(RSS) )

Feedback on the articles?

Hi all,

So I have four C++ articles posted so far(here). I have refreshed my memory on a lot of things by writing these, so this has been beneficial for me, as I hope it is for you. Any comments or questions? What would you like covered in the future? I do plan to put more examples where I have the same concepts in Objective-C and C++ in the same article, as well as providing some more examples of Objective-C++.

Regards,
Chinmoy.

total newbie to mac, old guy

total newbie to mac, old guy in Windows C++ C#.
Will Objective C++ programs also work on iPhone-iPod-iPad?
have lots of old code I would like to port, especially maths - vectors etc. I understand that the user interface and probably graphics, even though in OpenGL will have to be written in Objective C.
thanks

Just for the record,

Just for the record, Objective-C is *not* the only way to write user interfaces on the Mac!

I write all my code in FPC, which has good support for Cocoa. Even without such language specific support, it is perfectly possible (but harder) to use Cocoa, up to a point.

Yeah I use Fish Passage

Yeah I use Fish Passage Center's Foreign Press Center for all my Fortune Personal Consulting like on my last job with Fire Protection Consulting. FYI: People who use TLA's for obscure references IMO are TFA's.

----------------------------------------
Faith is believing in something you know isn't true.