Re: Obj-C++: instantiating C++ type ivar within an obj-c class
Re: Obj-C++: instantiating C++ type ivar within an obj-c class
- Subject: Re: Obj-C++: instantiating C++ type ivar within an obj-c class
- From: Chris Hanson <email@hidden>
- Date: Sun, 28 Aug 2005 12:07:57 -0700
On Aug 28, 2005, at 2:51 AM, Ben Dubin-Thaler wrote:
But I want to go further and actually use my C++ classes - in
particular a class called Array2D<type> (a 2d array template) - to
store some giant arrays of data as a private instance variable
(ivar) in my Obj-C class compiled with Obj-C++. The problem is that
when I pass the header of the model class to the controller class,
the controller, which is compiled as Obj-C, of course freaks
because it doesn't know about these weird C++ classes.
Header files in C, C++ and Objective-C are not independent from
source files. They're textually substituted into source files at
compilation time by the preprocessor. So if you use Objective-C++
constructs in a header, any file that includes it needs to be
compiled with the Objective-C++ compiler; the easiest way to do that
is to give it a ".mm" extension.
One thing you can do is encapsulate your C++ constructs even more
than you already have, so an SLContour has a pointer to a structure
that actually contains them (say a struct SLContourData) defined in
SLContourData.mm. Your struct SLContourData would just get a forward
declaration in SLContour.h. Nothing else would have to be Objective-C
++ then. In other words:
// SLContour.h
#import <Foundation/NSObject.h>
// Forward declaration so you don't need to include SLContourData.h:
struct SLContourData;
@interface SLContour : NSObject
{
NSMutableDictionary *parameters;
@private
struct SLContourData *contourData;
}
// etc.
@end
Of course, this means you'd have to do the memory management for
contourData in your -init and -dealloc methods too, but that's no big
deal...
-- Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden