<tfoot id='6fnvB'></tfoot>

    <small id='6fnvB'></small><noframes id='6fnvB'>

    <legend id='6fnvB'><style id='6fnvB'><dir id='6fnvB'><q id='6fnvB'></q></dir></style></legend>

          <bdo id='6fnvB'></bdo><ul id='6fnvB'></ul>
        <i id='6fnvB'><tr id='6fnvB'><dt id='6fnvB'><q id='6fnvB'><span id='6fnvB'><b id='6fnvB'><form id='6fnvB'><ins id='6fnvB'></ins><ul id='6fnvB'></ul><sub id='6fnvB'></sub></form><legend id='6fnvB'></legend><bdo id='6fnvB'><pre id='6fnvB'><center id='6fnvB'></center></pre></bdo></b><th id='6fnvB'></th></span></q></dt></tr></i><div id='6fnvB'><tfoot id='6fnvB'></tfoot><dl id='6fnvB'><fieldset id='6fnvB'></fieldset></dl></div>

        混合 C++ 和 Objective-C

        时间:2024-08-12

        <i id='CCTBP'><tr id='CCTBP'><dt id='CCTBP'><q id='CCTBP'><span id='CCTBP'><b id='CCTBP'><form id='CCTBP'><ins id='CCTBP'></ins><ul id='CCTBP'></ul><sub id='CCTBP'></sub></form><legend id='CCTBP'></legend><bdo id='CCTBP'><pre id='CCTBP'><center id='CCTBP'></center></pre></bdo></b><th id='CCTBP'></th></span></q></dt></tr></i><div id='CCTBP'><tfoot id='CCTBP'></tfoot><dl id='CCTBP'><fieldset id='CCTBP'></fieldset></dl></div>
        <tfoot id='CCTBP'></tfoot>

              <bdo id='CCTBP'></bdo><ul id='CCTBP'></ul>

              <small id='CCTBP'></small><noframes id='CCTBP'>

                    <tbody id='CCTBP'></tbody>
                • <legend id='CCTBP'><style id='CCTBP'><dir id='CCTBP'><q id='CCTBP'></q></dir></style></legend>
                • 本文介绍了混合 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:何时使用 CCSpriteBatchNode? 下一篇:如何检查设备是否有网络连接:cocos-2d

                  相关文章

                • <i id='anLVH'><tr id='anLVH'><dt id='anLVH'><q id='anLVH'><span id='anLVH'><b id='anLVH'><form id='anLVH'><ins id='anLVH'></ins><ul id='anLVH'></ul><sub id='anLVH'></sub></form><legend id='anLVH'></legend><bdo id='anLVH'><pre id='anLVH'><center id='anLVH'></center></pre></bdo></b><th id='anLVH'></th></span></q></dt></tr></i><div id='anLVH'><tfoot id='anLVH'></tfoot><dl id='anLVH'><fieldset id='anLVH'></fieldset></dl></div>
                  <tfoot id='anLVH'></tfoot>
                  <legend id='anLVH'><style id='anLVH'><dir id='anLVH'><q id='anLVH'></q></dir></style></legend>

                    1. <small id='anLVH'></small><noframes id='anLVH'>

                      • <bdo id='anLVH'></bdo><ul id='anLVH'></ul>