混合 C++ 和 Objective-C

Mixing C++ and Objective-C(混合 C++ 和 Objective-C)

本文介绍了混合 C++ 和 Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 C++ 作为应用主干,使用 Objective-C 作为 GUI,这很好.

I am using C++ as the app backbone and Objective-C for the GUI, that's fine.

但是在将这些代码混合到 Objective-C++(.mm 文件)中时,我有几个问题:

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 <boostshared_ptr.hpp>
@interface MyClass : NSObject {
  std::vector<boost::shared_ptr<CCSprite> > m_spriteList;
}

然后在.mm文件中,我想做

CCSprite* newSprite = [/* cocos2d stuff here... */];
m_spriteList.push_back(newSprite);

上面的代码有效吗?它当然是在 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++ 代码时,我想在 Objective-C 头文件中声明一个 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)
}

在上面的代码中,(2)可以吗?我可以像在 C++ 代码中一样在 Objective-C 中使用智能指针吗?我可以假设当 MyClass 对象被销毁时会调用 Test 类析构函数吗?

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) 可以吗?我需要手动调用吗delete m_testObjectPtr in 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类上使用then,你会得到编译错误或在某处崩溃.
您还可以使用带有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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:混合 C++ 和 Objective-C

基础教程推荐