• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Cocoa Classes from C++?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa Classes from C++?


  • Subject: Re: Cocoa Classes from C++?
  • From: Jens Alfke <email@hidden>
  • Date: Tue, 27 May 2008 20:43:09 -0700


On 27 May '08, at 4:50 PM, Todd Heberlein wrote:

(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;

I came up with an idiom to avoid this. Let's say the C++ class you want to use is MyCppClass. You declare the Objective-C class that uses it as:


// MyObjCClass.h

#ifdef __cplusplus
class MyCppClass;
#else
typedef struct MyCppClass MyCppClass;
#endif

@interface MyObjCClass {
	@private
	MyCppClass *cppObject;
}
....
@end

This basically tells a white lie to any non-C++-savvy source file that imports the header, telling them that MyCppClass* is just some kind of pointer, and not to worry its pretty little head about exactly what it is. This is safe because (a) all pointers are the same size, so the ivar layout is correct; and (b) no one but MyCppClass.mm can actually access the raw instance variable anyway.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Cocoa Classes from C++?
      • From: Bill Bumgarner <email@hidden>
References: 
 >Cocoa Classes from C++? (From: "J. Todd Slack" <email@hidden>)
 >Re: Cocoa Classes from C++? (From: Andrew Farmer <email@hidden>)
 >Re: Cocoa Classes from C++? (From: Jens Alfke <email@hidden>)
 >Re: Cocoa Classes from C++? (From: Todd Heberlein <email@hidden>)

  • Prev by Date: Question concerning mouseDragged events.
  • Next by Date: Re: Question concerning mouseDragged events.
  • Previous by thread: Re: Cocoa Classes from C++?
  • Next by thread: Re: Cocoa Classes from C++?
  • Index(es):
    • Date
    • Thread