使用 C++ 来自 Objective C:如何分配/解除分配?
目前,我的 Objective C 类通过在创建所有者时执行 new
操作,并在销毁所有者时调用 delete
来使用 C++ 对象。 但还有别的办法吗? 我希望能够声明一个 auto_ptr
,其作用域持续 Objective C 类的生命周期。
Currently, my Objective C classes use C++ objects by doing a new
when the owner is created, and calling delete
when it is destroyed. But is there another way? I'd like to be able to declare, say, an auto_ptr
whose scope lasts the duration of the Objective C class' lifetime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,让我假设您在 Mac 上使用 C++ 和 Objective-C,如果我是对的,您可能会使用 X 代码。 因此,如果您转到项目的属性(信息)。 您可以检查编译选项(GCC)。 在那里,有一个选项可以启用 C++ 构造函数/析构函数(在 Cocoa 项目中默认关闭)。
然后你会得到类似 C++ 的默认作用域,但是我没有太多使用它,而且我在使用大量模板代码(Boost)时遇到了问题。
另外,我认为除了一些在 GCC 工作的好人之外,没有人正式支持这一点。 因此,我建议您对类似的内容进行单元测试,并注意任何事情都可能出错。
尽管如此,对于我这个 C++ 人员来说,能够在 Objective-C 中使用 C++ 是一种解脱,而且风险值得收益:)
Ok, let me assume you are using C++ and Objective-C on a Mac, if I'm right you are likely using X-code. So if you go to the properties (info) of your project. You can check the compile options (GCC's). In there, there is an option to enable C++ constructors/destructors (which is turned off by default in Cocoa projects).
Then you get default-like C++ scoping, however I haven't used it much and I've had problems with heavily template code (Boost).
Also I don't think anyone officially supports this besides some good souls working on GCC. So I'd recommend that you unit test anything like this, and keep note that anything could go wrong.
Nevertheless being able to use C++ in objective-C, for me as a C++ person, is a relief and the risks are worth the benefits :)
如果您有哪怕一丝希望保留我们作为开发人员所留下的那点理智,您就不会这样做。 最好
删除
您的 C++ 对象。 一般来说,虽然逐行混合 Objective-C 和 C++ 是安全的,但不要指望运行时支持像混合生命周期这样奇特的事情。 一般来说,当你的 obj-c 类的 dealloc 被调用时,你可以安全地销毁你的对象,但除此之外,不要期望混合类作用域并且不要哭泣。If you have even the slightest hope of retaining what little sanity that we as developers have left you won't do that. It is best to
delete
your C++ objects. In general, while it is safe to mix Objective-C and C++ on a line-by-line basis, do not expect the runtime to support doing something fancy like mixing lifetimes. In general, you can safely destroy your objects when your obj-c class's dealloc is called but other than that, do not expect to mix class scope and not cry.在 Xcode 中,我正在阅读“The Objective-C 编程语言”,标题为“Using C++ With Objective-C”的部分。 我还没有尝试过,但它说你可以使用C++类作为实例变量。 它使用零参数构造函数来初始化 C++ 类的任何实例变量。 在 dealloc 中,析构函数以相反的实例变量声明顺序调用。
我刚刚遇到 OCPtr 以及对 Boost::shared_ptr with Cocoa 的评论。 两者都使用智能引用计数指针(为您管理赋值等运算符的所有引用计数的指针)。
In Xcode I am reading "The Objective-C Programming Language", the section titled "Using C++ With Objective-C". I have not tried it, but it says you can use C++ classes as instance variables. It uses the zero argument constructor to initialize any instance variables that are C++ classes. In dealloc the destructors are called in reverse instance variable declaration order.
I just came across OCPtr and a comment on Boost::shared_ptr with Cocoa. Both use a smart reference counting pointer (one that manages all the reference counting for you for assignment etc operators).