 |
 |
How can I use C++ with a Cocoa app?
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2001
Status:
Offline
|
|
I'm creating a small Cocoa app to draw some pixels in subclass of an NSView, and I want to use C++ in the project for the backend computational stuff.
How can I use a C++ class in, say, MyNSView.m? I figure that all I want to do is instantiate an object of one of my C++ classes (maybe handing it a pointer to an array to store pixel data).
Can anyone please direct me to a tutorial which covers mixing C++ in with a Cocoa app project?
[Edit] Whoops. I just found this:
file:///Developer/Documentation/ReleaseNotes/Objective-C++.html
I'll have to read that in the morning.
Any tips anyone can offer (not covered in the doc) are still appreciated. Ex. any PB-specific pointers?
(Last edited by greenOne; Oct 18, 2003 at 01:26 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
Basically, just rename your source files to .mm instead of .m and it will Just Work™. You may have noticed that the syntax of Objective-C doesn't overlap with that of C++ at all..
Example of using C++ in ObjC:
Code:
#include "ACPPClass.h"
@implementation SomeClass
- (void)callCPPStuff:(int)foo {
ACPPClass *temp = new ACPPClass([anotherClass getSomeValue:foo]);
temp->performAUsefulTask();
}
@end
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2002
Status:
Offline
|
|
Originally posted by Gul Banana:
Basically, just rename your source files to .mm instead of .m and it will Just Work™. You may have noticed that the syntax of Objective-C doesn't overlap with that of C++ at all..
there are some overlaps, but they aren't major. they both use the 'class' keyword, but that can be overcome.
|
|
dont phear if ya hear a fourin soun' to ur ear. iss al'ight mah.
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by thuhFreak:
there are some overlaps, but they aren't major. they both use the 'class' keyword, but that can be overcome.
Objective-C does not use the keyword class. It does use the keyword Class, and there is a method named class, but neither of these should create a conflict because the former is a completely different name and the latter is in a different namespace.
There is one actual overlap in syntax that I know of, though -- the syntax for a templatized class in C++ and the syntax for declaring that an object conforms to a protocol in Obj-C are the same. Apple gets around this by not allowing id to be used as a template name.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2002
Status:
Offline
|
|
Originally posted by Chuckit:
Objective-C does not use the keyword class. It does use the keyword Class, and there is a method named class, but neither of these should create a conflict because the former is a completely different name and the latter is in a different namespace.
There is one actual overlap in syntax that I know of, though -- the syntax for a templatized class in C++ and the syntax for declaring that an object conforms to a protocol in Obj-C are the same. Apple gets around this by not allowing id to be used as a template name.
well, `class' itself may not technically be a keyword in objc, but then '@class' is, where 'class' is a keyword for c++. both objc and c++ both use a similar syntaxes for forward declarations of classes. and both are similar to c's forward declaration of structs.
Code:
@class className; /* objc */
class className; // c++
struct structName; /* c */
i was merely trying to contend with the totality of Gul Banana's claim. i'm pretty sure there are a few things which overlap between objc and c++. but they are rare, and can almost definitely be avoided/worked-around.
|
|
dont phear if ya hear a fourin soun' to ur ear. iss al'ight mah.
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Oct 2003
Status:
Offline
|
|
Well, I started that greenOne account a long while back, and I'm trying to consolidate usernames :) so here I am (hopefully for a while) as johnMG.
Thanks for the tips. It works great for me.
I created a Cocoa app and then used IB to add a button to my plain-vanilla window. I then told IB that there'd be a new Obj-C class to connect the button to (Foo) and told it to create my files (Foo.m and Foo.h). After connecting the button to a member function ^H^H^H... method ^H^H^H... action :) of Foo, I quit IB.
In my project I added existing Bar.hpp and Bar.cpp (C++ files), then had the button action/method in Obj-C class Foo create a C++ Bar object and tell it to do a few things.
The one thing that popped up was: I'd done a build somewhere along the way that created my precompiled header ... Then, after right-clicking and changing my filename Foo.m --> Foo.mm, there was some build problem and I had to clean then build, and it then worked fine. :)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
thuhfreak: ah, but keywords are of a piece. "class" being one does not prevent "@class" from being one. I'd be interested to see if you can find any actual examples of overlap 
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|