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.
目前,我的Objective C类通过在创建所有者时执行新操作来使用C ++对象,并在销毁时调用delete。但还有另外一种方法吗?我希望能够声明一个auto_ptr,其范围持续Objective C class的生命周期。
3 个解决方案
#1
3
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).
好吧,我假设您在Mac上使用C ++和Objective-C,如果我是对的,您可能正在使用X代码。因此,如果您转到项目的属性(信息)。您可以检查编译选项(GCC)。在那里,有一个选项可以启用C ++构造函数/析构函数(在Cocoa项目中默认关闭)。
Then you get default-like C++ scoping, however I haven't used it much and I've had problems with heavily template code (Boost).
然后你得到类似默认的C ++范围,但是我没有用太多,而且我遇到了大量模板代码(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.
除了一些在GCC工作的好人之外,我认为没有人正式支持这一点。所以我建议你对这样的事情进行单元测试,并注意一切都可能出错。
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 :)
尽管如此,能够在Objective-C中使用C ++,对于我来说,作为一个C ++人,是一种解脱,风险是值得的好处:)
#2
1
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.
如果你有一丝希望保留我们作为开发人员留下的那么小的理智,你将不会那样做。最好删除C ++对象。一般来说,尽管在逐行的基础上混合使用Objective-C和C ++是安全的,但是不要指望运行时支持像混合生命周期那样花哨的东西。通常,在调用obj-c类的dealloc时可以安全地销毁对象,但除此之外,不要期望混合类范围而不是哭。
#3
0
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.
在Xcode中,我正在阅读“Objective-C编程语言”,标题为“使用带有Objective-C的C ++”一节。我没有尝试过,但它说你可以使用C ++类作为实例变量。它使用零参数构造函数初始化任何C ++类的实例变量。在dealloc中,析构函数以反向实例变量声明顺序调用。
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).
我刚刚遇到OCPtr和Boost :: shared_ptr与Cocoa的评论。两者都使用智能引用计数指针(管理所有引用计数的指针用于赋值等操作符)。
#1
3
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).
好吧,我假设您在Mac上使用C ++和Objective-C,如果我是对的,您可能正在使用X代码。因此,如果您转到项目的属性(信息)。您可以检查编译选项(GCC)。在那里,有一个选项可以启用C ++构造函数/析构函数(在Cocoa项目中默认关闭)。
Then you get default-like C++ scoping, however I haven't used it much and I've had problems with heavily template code (Boost).
然后你得到类似默认的C ++范围,但是我没有用太多,而且我遇到了大量模板代码(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.
除了一些在GCC工作的好人之外,我认为没有人正式支持这一点。所以我建议你对这样的事情进行单元测试,并注意一切都可能出错。
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 :)
尽管如此,能够在Objective-C中使用C ++,对于我来说,作为一个C ++人,是一种解脱,风险是值得的好处:)
#2
1
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.
如果你有一丝希望保留我们作为开发人员留下的那么小的理智,你将不会那样做。最好删除C ++对象。一般来说,尽管在逐行的基础上混合使用Objective-C和C ++是安全的,但是不要指望运行时支持像混合生命周期那样花哨的东西。通常,在调用obj-c类的dealloc时可以安全地销毁对象,但除此之外,不要期望混合类范围而不是哭。
#3
0
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.
在Xcode中,我正在阅读“Objective-C编程语言”,标题为“使用带有Objective-C的C ++”一节。我没有尝试过,但它说你可以使用C ++类作为实例变量。它使用零参数构造函数初始化任何C ++类的实例变量。在dealloc中,析构函数以反向实例变量声明顺序调用。
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).
我刚刚遇到OCPtr和Boost :: shared_ptr与Cocoa的评论。两者都使用智能引用计数指针(管理所有引用计数的指针用于赋值等操作符)。