应提高:: ptr_vector到位的std ::矢量所有的时间使用? [英] Should boost::ptr_vector be used in place std::vector all of the time?

查看:175
本文介绍了应提高:: ptr_vector到位的std ::矢量所有的时间使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是,我已经运行到一个概念性的问题。在我当前的项目,感觉就像我过度使用升压 smart_ptr ptr_container 库。我创建的boost :: ptr_vectors
在许多不同的对象,并调用转移()方法来从一个移动某些指针的boost :: ptr_vector 到另一个。

Just a conceptual question that I've been running into. In my current project it feels like I am over-using the boost smart_ptr and ptr_container libraries. I was creating boost::ptr_vectors in many different objects and calling the transfer() method to move certain pointers from one boost::ptr_vector to another.

这是我的理解是,重要的是要清楚地显示堆中分配的对象的所有权。

It is my understanding that it is important to clearly show ownership of heap allocated objects.

我的问题是,它希望利用这些Boost库来创建属于对象堆上分配的成员,但然后通过获取正常使用指针这些成员()做任何处理的时候。

My question is, would it be desirable to use these boost libraries to create heap-allocated members that belong to an object but then use normal pointers to these members via get() when doing any processing.

例如...
一个游戏可能有属于它的瓷砖的集合。它可能是有意义的以创建这些瓷砖的boost :: ptr_vector 。当比赛结束后,这些砖应自动释放。

For example... A game might have a collection of Tiles that belong to it. It might make sense to create these tiles in a boost::ptr_vector. When the game is over these tiles should be automatically freed.

但是,如果我想要把这些瓷砖在一个袋子对象暂时,我应该创造收入囊中另一个的boost :: ptr_vector ,并通过游戏的瓷砖转移到袋转移()
我应该创建一个的std ::矢量<瓷砖* GT; 其中,瓷砖*的参考瓷砖
在游戏中,并传递给袋?

However if I want to put these Tiles in a Bag object temporarily, should I create another boost::ptr_vector in the bag and transfer the Game's Tiles to the Bag via transfer() or should I create a std::vector<Tile*> where the Tiles*'s reference the Tiles in the Game and pass that to the Bag?

感谢。

**编辑
我要指出的是,在我的例子,比赛将有一个袋子对象作为成员。包里只装满瓷砖游戏拥有。所以袋不会没有游戏存在。

**Edit I should point out that in my example The Game would have a Bag object as a member. The Bag would only be filled with Tiles the game owns. So the Bag would not exist without the Game.

推荐答案

您应该只使用拥有智能指针和指针容器那里的明确所有权的传输的。如果对象是暂时的还是不没关系 - 所有重要的是它是否具有所有权或没有(因此,previous所有者是否放弃所有权)

You should only use owning smart pointers and pointer containers where there's clear transfer of ownership. It doesn't matter if the object is temporary or not - all that matters is whether it has ownership or not (and, therefore, whether the previous owner relinquishes ownership).

如果您创建的指针的临时矢量只是将它传递给一些其他的功能,和原来的 ptr_vector 仍然引用所有对象,有没有过户,因此,你应该使用普通的矢量的临时对象 - 就像你使用原始指针从 ptr_vector 到需要一个指针,但不会将其存储在任何地方。一个函数

If you create a temporary vector of pointers just to pass it to some other function, and the original ptr_vector still references all those objects, there's no ownership transfer, and therefore you should use plain vector for the temporary object - just as you'd use a raw pointer to pass a single object from ptr_vector to a function that takes a pointer, but doesn't store it anywhere.

这篇关于应提高:: ptr_vector到位的std ::矢量所有的时间使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆