Am 28.05.2008 um 01:50 schrieb Todd Heberlein:
The gotchas that I often run into are: (1) Changing an Objective-C file to an Objective-C++ object (by renaming it to a .mm file) often causes me to rename a lot of files to .mm, because if the Objective- C class definition has a C++ object in it, every source code file that includes that Objective-C class definition needs to be renamed to a .mm file;


 You can also do something like the following in the header:

struct MyCppIVars;

@interface MyCppUsingClass : NSObject
{
        struct MyCppIVars*      cppIVars;
}

@end

and then in the source file:

struct MyCppIVars
{
        std::vector<int>  myInts;
};

@implementation MyCppUsingClass

-(id) init
{
        self = [super init];
        if( self )
        {
                cppIVars = new MyCppIVars;
        }
        return self;
}

-(void) dealloc
{
        delete cppIVars;
        cppIVars = NULL;
        [super dealloc];
}

@end

The headers only need to know it's a pointer, but doesn't have to know there's C++ stuff in it, and this way, any ivars you add to the struct are automatically constructed/destructed without the need to add more new/delete calls. This is handy boilerplate code. Also if you put all ivars (not just the C++ ones) in the struct, it avoids the fragile base class problem if you're exposing the ObjC class to plugins or whatever.

(2) I always declare my C++ objects in Objective-C classes as pointers, and then in the init method I allocate the C++ object;

One trick I've seen here is to have a #ifdef __cplusplus check there, and then to typedef the C++ types to void* for pure-C or pure- ObjC callers.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de





_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to