Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mac > BOOK: Beginning Mac OS X Programming
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Mac OS X Programming
This is the forum to discuss the Wrox book Beginning Mac OS X Programming by Michael Trent, Drew McCormack; ISBN: 9780764573996
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Mac OS X Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 30th, 2005, 02:39 PM
Registered User
Join Date: Sep 2005
Location: , , USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Objective-C & C++ frameworks

Can one use C++ frameworks from Objective-C code?

  #2 (permalink)  
Old September 30th, 2005, 05:09 PM
Registered User
Join Date: Sep 2005
Location: , , USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

I am answering my own question: Chapter 9 promises to explain how to call Carbon APIs from Cocoa applications.
I would like to write a MIDI application using Cocoa, but as near as I can tell at this point, the MIDI routines
are Carbon and written in C++. Am I making sense?

  #3 (permalink)  
Old October 2nd, 2005, 03:18 AM
Authorized User
Join Date: Aug 2005
Location: La Charité-sur-Loire, , France.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts

Yes, you are making sense... But I am reading Chapter 6 now! However, I think you can write a part of program in a source file in C++ and the compiler, then linker know what they have to do with, but I have not tryed this. It seems to me I have read something about this in the Apple doc.

  #4 (permalink)  
Old November 1st, 2005, 06:55 PM
Wrox Author
Join Date: Nov 2005
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts

You can use C++ from within Objective-C code using the Objective-C++ GCC front-end. This allows you to mix Objective-C and C++ with some restrictions. If you give your source files a ".mm" extension (instead of ".m" or ".cpp") Xcode will use this Objective-C++ front-end.

One thing to remember is that Objective-C objects in a Objective-C++ file are still just Objective-C objects, not C++ classes. You must alloc/init them as usual; you cannot "new" them. The same is true of C++ objects; they must be created on the stack, or you must use "new" to create them in the heap. You cannot (last I checked) embed C++ objects inside an Objective-C object directly; although you can include pointers to C++ objects in your Objective-C object.

For example, you can't:

@interface ObjCObject : NSObject
    CppClass mClass;
But you can:

@interface ObjCObject : NSObject
    CppClass *mClass;
Another way of mixing C++ and Objective-C is to wrap your C++ code in C bindings. Your C++ code lives in a ".cpp" file, and your Objective-C object lives in a ".m" file. Your C++ header file would only include C functions (i.e., using the "extern "C" {} " directive) implemented in the C++ file. This is kind of tedious because you have to maintain two interfaces for your C++ objects; the public C interface and the private C++ interface. You also lose some of the benefits of C++ at the API level (polymorphism, inheritance) although you also protect yourself against other problems (fragile base-classes, etc.).

Which path you choose will depend entirely on your situation.

A quick Google search turned up an online tutorial for using Objective-C++ in Xcode. I haven't looked it over in detail, but it's probably a good place to start looking.


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Portal Frameworks and Web Parts!!! msdnman BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 8 March 4th, 2007 12:29 AM
Javascript && keeps turnig into && ayrton Pro VB.NET 2002/2003 3 June 27th, 2005 03:34 PM
Linux & KDE & C++ & QT & MYSQL & Kdevelop Munnnki Linux 0 January 2nd, 2005 05:41 PM
sql & join tables & find a field in multiple table trangd Beginning PHP 2 January 29th, 2004 07:18 PM

All times are GMT -4. The time now is 04:14 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.