Possibly Stupid newb questions... forgive me!

Nibbie
Posts: 2
Joined: 2008.12
Post: #1
Howdy

I've started fiddling with some Mac OSX programming after getting bored with Windows, but there's something that I can work out (actually lots of things, but this one's stopping me going further for now):

How do I use a normal C++ class variable in one of the main NSView class thingys?

It seems in a .m file (objective-c) it will always complain about the class being defined in an #include'd file. If it's a .mm file, it compiles fine, but then I get a single error that makes no sense. It simply says "Undefined Symbols:" without actually saying what symbols are undefined. Obviously, simply renaming the file to .mm isn't the way to go. Smile But I can't find any of the files I can add to xcode, or any of the projects you start with, that let you start with a .mm file anyway.

So question is, how do I use C++ classes in a view class. I'm starting by trying to do a screensaver. So I've started a Screen Saver project, no worries there. But I'm using code I've written in c++ for the drawing and all that, so I need to have a variable of that class in the view class.

If it makes any diff, I'm using OpenGL for drawing stuff, but I'm pretty sure all the foundations I need are included, so I don't understand the empty "Undefined symbols:" message when I actually get it to compile. Help!

I hope this makes sense to someone, and I apologise for being clueless!

Rob.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
if you drag up the divider at the bottom of the build errors window, you can see the detailed build log. the undefined symbols will be listed there.

are you aware of the implications of storing a C++ object in a field of an Objective C class?
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #3
Well, if you want more detailed error messages, open up the Detailed Build Results window and look at the middle panel (you may have to open it up by clicking and dragging up on the circle in the middle -- hopefully that makes sense). That should tell you what your undefined symbol is. But normally, you should have no problems calling C++ code from within your Obj-C++ classes. I do it all the time.

Edit: Dammit, OSC! You only beat me this time to the punch because it's late and I'm tired and ripping CDs is slowing down the keyboard response time.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2008.12
Post: #4
Ahhh! The old hidden window on the error page trick! Can't believe I fell for it...

Thanks a lot. Worked like a charm. Strange problem tho. It was complaining about functions that did exist, but were in a .c file. I renamed it to .cpp and suddenly it had no problem finding them? So it all compiled fine. Thanks!

However, I can't say that I am aware of the implications of using cpp classes. Can you fill me in?

Thanks again.

Rob.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
C++ does symbol name mangling, C doesn't. That means you either need to tell the C++ compiler that the functions are C functions, or do as you did and compile them as C++...

If you have a C++ object as a field of an ObjC object, the C++ object's constructor won't be called when the ObjC object is created; you must use "placement new" to call it from the ObjC object's initializer. Also, the destructor won't be called when the ObjC object is destroyed, you must call it yourself from -dealloc.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
For what it's worth I've been able to use the vanilla "new" operator quite effectively in ObjectiveC++.

Apple has a document on writing ObejctiveC++ which goes over all the ins and outs. This worked well enough for me -- and I do a *lot* of ObjectiveC++.

The real thing to keep in mind is that stack allocated C++ objects are fine, so long as their scope is inside a function. But C++ objects which are members of an ObjectiveC class absolutely must be allocated on the heap, and deleted in your -dealloc method.

Don't worry, it's really not that scary.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2008.12
Post: #7
All good stuff to know. Thanks a lot.

So is it ok then to have a pointer in my Objc class like MYCLASS *mc, then allocate it with mc = new MYCLASS, and then in -dealloc have delete mc?

Am I on the right track? Will that call all the constructors and destructors?

Thanks once a again. I'm feeling like once I get my head around the little differences it'll all start falling into place.

Rob.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
yes, that's fine. it's only when you have a c++ object by value in an objc object that you have to worry about placement new and manual destructor calls.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2008.12
Post: #9
Good stuff. Thanks again.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Very stupid question about Python on OS X Durandal 10 8,420 Dec 17, 2008 09:33 AM
Last Post: Duane
  Stupid Radio Buttons! UselessRob 4 3,142 Apr 22, 2005 06:20 AM
Last Post: blobbo