混合C ++和Objective-C [英] Mixing C++ and Objective-C
问题描述
我使用C ++作为应用程序的骨干和Objective-C的图形用户界面,这很好。
I am using C++ as the app backbone and Objective-C for the GUI, that's fine.
但是,当涉及到在Objective-C ++(.mm文件)混合在一起的code,我有几个问题:
But when it comes to mixing those code together in Objective-C++ (.mm file), I have got a few question:
例如。在Objective-C的头,我可以做到以下几点?
E.g. In Objective-C header, can I do the following?
#include <vector>
#include <boost\shared_ptr.hpp>
@interface MyClass : NSObject {
std::vector<boost::shared_ptr<CCSprite> > m_spriteList;
}
然后在 .mm
文件,我想要做的。
CCSprite* newSprite = [/* cocos2d stuff here... */];
m_spriteList.push_back(newSprite);
是上面code有效吗?这当然是在C ++中,但混合C ++和Objective-C和Cocos2d的时候我不知道。
Is the above code valid? It certainly is in C++, but I am not sure when mixing C++ and Objective-C and Cocos2D.
当我尝试使用在Objective-C的C ++ code,我要声明一个C ++对象中的Objective-C头文件的成员变量。
When I try to use the C++ code in Objective-C, I want to declare a C++ object as a member variable in the Objective-C header file.
说我有在 test.h
头部声明C ++类:
Say I have a C++ class declared in the test.h
header:
Test{
};
在Objective-C头文件,我想要做的。
In Objective-C header file, I want to do
#include "test.h"
#incude <boost/scoped_ptr.hpp>
#include <vector>
@interface MyClass : NSObject {
Test* m_testObjectPtr; // (1)
boost::scoped_ptr<Test> m_testOjbSmartPtr; // (2)
}
在上面的code,为(2)行不行?我可以在C ++ code使用Objective-C的智能指针一样?而且我可以假设当 MyClass的
对象被销毁的测试
类析构函数将被调用?
In the above code, is (2) okay? Can I use smart pointers in Objective-C just like in C++ code? And can I assume the Test
class destructor will be called when the MyClass
object is destroyed?
或者,如果(2)是不行的在Objective-C ++,是(1)行不行?我会需要手动调用在
的dealloc
?
Or if (2) is not okay in Objective-C++, is (1) okay? Would I need to manually call
delete m_testObjectPtr
in dealloc
?
推荐答案
您只能在C ++类使用智能指针。如果你对Objective-C类,然后使用你要么得到的地方编译错误或崩溃。结果
您还可以使用与Objective-C类的指针容器,如
You can use smart pointer only on c++ classes. if you use then on objective-c classes you will either get compile error or crash somewhere.
You can also use containers with pointers of objective-c classes like
std::vector<CCSprite *> spriteList;
只要确保你留住他们,当你插入它们列出并释放他们,当你删除它们。结果
在这两种情况下,你可以让你自己的呼叫保留了智能指针,并在构造函数/自毁发布/复制一样需要再不用担心保留的释放。结果
此外析构函数成员C ++对象会被自动调用,当对象被释放。结果
客观C包装的一个例子是
just make sure you retain them when you insert them to list and release them when you remove them.
In both cases, you can make a smart pointer of your own that calls retain and release in constructor/destruct/copy like needed and then don't worry about retain release.
Also destructor for member c++ objects will be called automatically when the object is deallocated.
An example of an objective c wrapper would be
template<typename T>
struct shared_objc_object
{
T *Object;
shared_objc_object : Object(nil) { }
shared_objc_object(T *Object) : Object([Object retain]) { }
shared_objc_object(shared_objc_object &other) :
Object([other.Object retain]) { }
~shared_objc_object() { [Object release]; }
shared_objc_object &operator =(shared_objc_object &other)
{
[Object release];
Object = [other.Object retain];
}
}
您还可以使用
std::vector<shared_objc_object<CCSprite *>> spriteList;
spriteList.push_back(some_sprite);
和不关心保留/释放
这篇关于混合C ++和Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!