Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Mac Programmer Help > Re: Objective-C...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 8 Topic 3791 of 3882
Post > Topic >>

Re: Objective-C++ Instance Variables

by William Woody <woody@[EMAIL PROTECTED] > Jul 3, 2008 at 05:16 PM

> I'm trying to bridge between my Cocoa App and my C++ framework via
> Objective-C++. Apparently I can't have C++ objects as instance
> variables in my ObjC class because they contain virtual methods. Also,
> I don't appear to be able to include my C++ header files in by ObjC
> header file; they work fine if I include them in my implementation
> file (.mm).
> 
> How should I create my C++ objects and use them within my ObjC class
> (over it's lifetime)?  Perhaps I should be doing this a different way?
> What I have is a C++ framework compiled into a dylib. I include the
> headers in my .mm and link in the dylib.


What I've done is create a pointer in my Objective C class which points 
to my C++ class:

@[EMAIL PROTECTED]
 ObjCClass : NSObject
{
   CPPClass *fPtr;
}

- (id)init;
- (void)dealloc;

@[EMAIL PROTECTED]
 ObjCClass

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

- (void)dealloc
{
   if (fPtr) delete fPtr;
   [super dealloc];
}

@[EMAIL PROTECTED]
 that this implies that you either #include the C++ header in your 
Objective C header, or you need to declare the CPPClass in the Objective 
C header as a anonymous C++ class. In either case, this means you can 
only #im****t your Objective C header in a .mm file.

There are some places where things can get a little mind-bending, by the 
way; in a few places in my code I've written things like:

   std::map<int32_t,MyStruct> array;
   ...
   return array[[myObjC count]];

Where the outer [] is the C++ operator [] call, and the inner [] is an 
Objective C method call.


-- 
William Edward Woody   -   woody@[EMAIL PROTECTED]
 In Motion        -   http://www.chaosinmotion.com

Freedom is the non-negotiable demand of human dignity; 
the birthright of every person‹-in every civilization.
            - National Security Strategy of the United States
 




 8 Posts in Topic:
Objective-C++ Instance Variables
Jason8 <jason.leach@[E  2008-07-03 00:11:05 
Re: Objective-C++ Instance Variables
Gregory Weston <uce@[E  2008-07-03 07:52:36 
Re: Objective-C++ Instance Variables
Jason8 <jason.leach@[E  2008-07-03 07:44:01 
Re: Objective-C++ Instance Variables
Gregory Weston <uce@[E  2008-07-03 12:31:13 
Re: Objective-C++ Instance Variables
William Woody <woody@[  2008-07-03 17:16:40 
Re: Objective-C++ Instance Variables
David Phillip Oster <o  2008-07-03 20:46:53 
Re: Objective-C++ Instance Variables
William Woody <woody@[  2008-07-04 16:24:53 
Re: Objective-C++ Instance Variables
David Phillip Oster <o  2008-07-04 19:48:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 10:40:37 CST 2008.