混合 C++和 Objective-C

发布于 2024-12-17 05:03:20 字数 1317 浏览 3 评论 0原文

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

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

1. 我可以将 STL 容器与 Objective-C 或 Cocos2D 对象混合在一起吗?

例如,在 Objective-C 标头中,我可以执行以下操作吗?

#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);

是上面的代码有效吗?它当然是在 C++ 中,但我不确定何时混合 C++ 和 Objective-C 和 Cocos2D。

2. Objective-C中使用C++智能指针对象进行内存管理?

当我尝试在 Objective-C 中使用 C++ 代码时,我想在 Objective-C 头文件中声明一个 C++ 对象作为成员变量。

假设我在 test.h 标头中声明了一个 C++ 类:

Test{
};

在 Objective-C 标头文件中,我想做

#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 类析构函数吗?

或者,如果 (2) 在 Objective-C++ 中不行,那么 (1) 可以吗?我需要手动调用吗 删除dealloc中的m_testObjectPtr?

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

But when it comes to mixing those code together in Objective-C++ (.mm file), I have got a few question:

1. Can I mix STL containers with Objective-C or Cocos2D objects?

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;
}

And then in the .mm file, I want to do

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

Is the above code valid? It certainly is in C++, but I am not sure when mixing C++ and Objective-C and Cocos2D.

2. Memory management using C++ smart pointer object in 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.

Say I have a C++ class declared in the test.h header:

Test{
};

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)
}

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?

Or if (2) is not okay in Objective-C++, is (1) okay? Would I need to manually call
delete m_testObjectPtr in dealloc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-12-24 05:03:20

您只能在 C++ 类上使用智能指针。如果你在 Objective-C 类上使用 then ,你要么会遇到编译错误,要么会在某个地方崩溃。
您还可以使用带有 Objective-C 类指针的容器,例如

std::vector<CCSprite *> spriteList;

确保在插入它们以列出它们时保留它们,并在删除它们时释放它们。
在这两种情况下,您都可以创建自己的智能指针,根据需要在构造函数/析构/复制中调用保留和释放,然后不必担心保留释放。
当对象被释放时,C++ 成员对象的析构函数也会被自动调用。
Objective C 包装器的一个例子是

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);

并且不关心保留/释放

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;

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];
    }
}

And you can use

std::vector<shared_objc_object<CCSprite *>> spriteList;
spriteList.push_back(some_sprite);

and don't care about retain/release

烟雨凡馨 2024-12-24 05:03:20

您需要注意一些问题。 C++ 类不享有与您在成为 Objective-C++ 对象的类成员时可能习惯的相同的基于作用域的生命周期。当alloc/initing时,构造函数不会被调用,当release时,析构函数也不会被调用,除非你小心就地使用 new/delete 或保留指针并使用 new/delete 显式管理它。

另外,如果 Objective-C++ 标头需要与 Objective-C 文件共享,则根本不能使用任何 C++ 结构。通过使用 pimpl 模式隐藏所有 C++ 成员,可以缓解这两个问题。

我可以将 STL 容器与 Objective-C 或 Cocos2D 对象混合吗?

是的,由于 Objective-C 对象只是指向结构的指针,因此您可以轻松地将它们存储在 STL 容器中,甚至可以向前声明类型并传递它转化为纯C++代码。 (请注意,如果没有棘手且脆弱的代码,C++ 代码实际上无法使用指针做很多事情,但是稍后您始终可以将指针传递回 Objective-C 代码以完成有用的工作。)

使用 C++ 智能进行内存管理Objective-C 中的指针对象?

您可以使用智能指针来管理 Objective-C 对象的生命周期,但您需要注意它们不会调用 delete (默认)大多数 C++ 智能的行为指针)。使用 C++11 或 boost 中的shared_ptr,您可以提供自定义删除器;尽管现在你有两个引用计数系统。您可以使用 boost::intrusive_ptr 来跳过额外的开销并直接使用 Objective-C 的引用计数。

There are some issues you'll want to be aware of. C++ classes do not enjoy the same scope based lifetime you might be used to when made into class members of Objective-C++ objects. When alloc/initing, the constructor won't be called, and when releasing, the destructor won't be called, unless you carefully use in place new/delete or hold on to a pointer and explicitly manage it with new/delete.

Also, if the Objective-C++ header needs to be shared with Objective-C files, you cannot use any C++ constructs at all. Both problems can be mitigated by hiding all C++ members using the pimpl pattern.

Can I mix STL containers with Objective-C or Cocos2D objects?

Yes, since Objective-C objects are just pointers to structs, you can store them easily in STL containers and even forward declare the type and pass it into pure C++ code. (Note, the C++ code can't really do much with the pointer without tricky and brittle code, but you can always pass the pointer back into Objective-C code later to get useful work done.)

Memory management using C++ smart pointer object in Objective-C?

You can use smart pointers to manage the lifetime of your Objective-C objects, but you will need to be careful that they do not call delete (the default behavior for most C++ smart pointers). With shared_ptr from C++11 or boost, you can provide a custom deleter; though now you have two reference counting systems. You can instead use boost::intrusive_ptr to skip that extra overhead and use Objective-C's reference counting directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文