开发者

c++ class object as an instance variable in obj-c class

开发者 https://www.devze.com 2023-02-22 03:58 出处:网络
I have a c++ class IPoint and want to use an instance of IPoint as an instance variable for an obj-c class.

I have a c++ class IPoint and want to use an instance of IPoint as an instance variable for an obj-c class. But when compiling it gives me the error: "expected specifier-qualifier-list before 'Ipoint'" However I have included the desired header that is "IPoint.h".

But when I use the cpp object in my class just by initializing it without making it an instance variable, it works.

There is a specific requirement of the cpp object to be stored as instance variable since it is required further in my project, if there could开发者_JAVA百科 be a way to make it work like writing a wrapper for the object or anything else. kindly help me out!


If you want to be able to #import your Objective-C class interface into both Objective-C and Objective-C++ code, you can use an #ifdef to declare the instance variable as void* for the former:

@interface MyClass : NSObject {
    #ifdef __cplusplus
    IPoint *point;
    #else
    void *point;
    #endif
}

Unfortunately, this does mean you'll need to manage the lifetime of the C++ object manually, creating and destroying it with new and delete in your Objective-C++ init and dealloc methods, respectively.


The header for an Objective-C class with C++ class ivar must be compiled as Objective-C++. Make sure to use .mm extension instead of .m extension in the source code filename, or set the file to compile as Objective-C++ in Xcode.

0

精彩评论

暂无评论...
验证码 换一张
取 消