使用 C++ 来自 Objective C:如何分配/解除分配?

发布于 2024-07-08 14:11:41 字数 172 浏览 14 评论 0原文

目前,我的 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 技术交流群。

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

发布评论

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

评论(3

完美的未来在梦里 2024-07-15 14:11:41

好吧,让我假设您在 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 :)

仙气飘飘 2024-07-15 14:11:41

如果您有哪怕一丝希望保留我们作为开发人员所留下的那点理智,您就不会这样做。 最好删除您的 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.

深者入戏 2024-07-15 14:11:41

在 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).

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