类成员变量有哪些非共享智能指针
当我有一个包含指针作为成员变量的类时,如果我不想使用普通指针,它们应该具有什么类型的智能指针?它们不需要共享(因此不需要shared_ptr)。 scoped_ptr 不起作用,因为我经常需要在初始化列表之外构建对象。
或者,在创建过程中当某些事情仍然可能失败(抛出异常等)时使用scoped_ptr,然后将它们分配给普通指针,这可能是常见的做法吗?
When I have a class that contains pointers as member variables what type of smart pointer should they have if I want don't want to use plain pointers? They do not need to be shared (so no shared_ptr necessary). scoped_ptr won't work since I often need to build the objects outside of the initialization list.
Or is it maybe common practice to use a scoped_ptr during the creation when something can still fail (exceptions thrown etc.) and afterwards assign them to plain pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只是想将成员指针存储在智能指针类型类中,以便您不能/不会忘记删除它们,那么标准选择是
auto_ptr
。它位于 STL 中,当您需要释放分配给它的当前内存并用新对象替换它时,可以使用reset()
函数轻松“重置”它。您仍然希望为具有 auto_ptr 成员的类实现自己的复制构造函数和赋值运算符。这是因为 auto_ptrs 赋值运算符转移了底层对象的所有权,因此默认赋值运算符不会达到您想要的效果。
该类可能如下所示:
对于所有其他情况,我建议使用
boost::shared_ptr
。 Shared_ptr 确实进行引用计数,但您可以将它们存储在标准容器中,这使得它们非常有用。您最终应该尝试摆脱对任何指向负责删除的已分配内存的任何内容使用普通指针。如果您想使用普通指针来访问或迭代普通 ole 数组等,那么没问题(但问问自己为什么不使用 std::vector),但是当您使用它们指向某个东西时它负责释放,那么你就是在自找麻烦。我编写代码时的目标是没有显式删除。
If you're just wanting to store member pointers in a smart pointer type class so you can't/won't forget to delete them, then a standard choice would be
auto_ptr
. It's in the STL and is easily "reset" with thereset()
function when you need to release the current memory allocated to it and replace it with a new object.You will still want to implement your own copy constructor and assignment operators for the classes which have auto_ptr members. This is due to the fact that auto_ptrs assignment operator transfers ownership of the underlying object so a default assignment operator will not have the effect you want.
Here is what the class might look like:
For all other cases I would suggest
boost::shared_ptr
. Shared_ptr does do reference counting but you can store them in standard containers which makes them quite useful.You should ultimately try to rid yourself of using plain pointers for anything which points at allocated memory it's responsible for deleting. If you want to use a plain pointer for accessing or iterating over a plain ole array etc., then that's fine (but ask yourself why you're not using a std::vector), but when you use them to point at something that it is responsible for freeing then you're asking for trouble. My goal when writing code is to have no explicit deletes.
您可以使用
std::auto_ptr
,它在 TR1 之前可用,因此您的代码不依赖于支持 TR1 智能指针的编译器。You could use
std::auto_ptr
, which was available prior to TR1 and therefore your code is not dependant on a compiler supporting TR1-smartpointers.通常我使用 deep_copy_ptr。现在我知道 loki smart_ptr 和 axter 智能指针可以做到这一点。它允许指针类自动复制,就像它是普通成员变量一样(您不需要定义特殊的赋值运算符/复制构造函数)。
我认为您不必在初始化列表中专门初始化它(但像普通指针一样,如果它没有有效值,显然不要使用它)。
Normally I use a deep_copy_ptr. Right now I know of loki smart_ptr and axter smart pointer that do this. It allow the pointer class to be automatically copied somewhat like if it was a normal member variable (you don't need to define a special assignment operator/copy constructor).
I think you don't have to specifically initialize it in the initializer list (but like a normal pointer, don't use it if it do not have a valid value, obviously).