Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting Objective-C++ to work



Francisco Tolmasky wrote:
| Ok, I got the spirit libraries to work fine with Project Builder, but
| now I'm trying to integrate the C++ portion of the program with the
| Objective-C part. Separately they work fine. I was hoping the
| Objective-C++ thing would make it seemless but I'm having some trouble.

You're asking for more than is promised. "Objective-C++" means only that a single source file can use both Objective-C and C++ syntax at the same time. Beyond that, things are far from seamless. (There are a variety of seemingly-innocuous things you can do that will produce nasty, hard-to-find bugs.)

| For some reason I get many weird errors when I try to combine them. I
| *think* this stems from the fact that for some reason, things like
|
| #include <iostream>
|
| are not supported in my Cocoa Project.

They are certainly supported by PB and gcc. I use C++ headers in Objective-C++ code routinely.

| iostream.h works fine, but
| whenever I put <iostream> it says file not found. I can't go into all
| the boost/spirit libraries and change them,

Nor should you. "Iostream.h" is long obsolete. (The Boost people know what they're doing.)

| so i was wondering what I
| might be doing wrong.

Since we don't know what you're doing, it's hard to say. The most likely possibility is that your project doesn't tell PB where to find the C++ headers. But it might be something else. Without knowing how your project is set up, what versions of software you're using, and so on, we can't really say.

| [A]re there any more
| things I should concern myself with when doing Objective-C++ (other
| than those explicitly listed on the information page).

As I don't know what page you consider to be "the information page", I can't say whether these things appear there or not. But the worst landmine I know of is this: The Objective-C and C++ runtime systems are completely ignorant of each other. In particular:
- Objective-C classes can't (easily) have members whose types are C++ classes.
(It's possible, but you have to call the constructor and destructor explicitly
in the init and dealloc methods.) Pointers to C++ classes are fine.
- C++ destructors aren't called when an Objective-C exception is raised.
- C++ exceptions aren't caught by NS_DURING blocks, and Objective-C exceptions
aren't caught by C++ "catch" blocks.

In addition, it's very much *not* safe to pass C++ objects to Objective-C methods, or to have an Objective-C method return a C++ object. The object is copied to or from the method using a bitwise copy; the class's copy constructor isn't invoked, nor is the class's destructor called for the copy.

I've also occasionally run into problems mixing the Objective-C "id" and "Class" types with C++ STL containers, none insurmountable.

Glen Fisher
_______________________________________________
projectbuilder-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/projectbuilder-users
Do not post admin requests to the list. They will be ignored.

References: 
 >getting Objective-C++ to work (From: Francisco Tolmasky <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.