Qt 和 auto_ptr
我刚刚发现了 auto_ptr 的概念并且很喜欢它!由于 Qt 通常需要 QList 或 QVector<(某些 QObject 或 QWidget)*>,是否有任何具体原因应该避免 auto_ptr 。如果我是对的,它允许你
std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size_t i=0; i<vec.length(); ++i)
{
delete vec[i];
}
vec.clear();
用更短的东西来替换它(即不需要清理)
std::vector<auto_ptr<MyClass>> vec;
/* add several elements to the vector and do stuff with them */
// no need for the delete loop
...Qt 仍然可以使用 auto_ptr 来发挥其共享内存魔力吗?父子自动内存管理是否仍然透明地发挥作用?谢谢
I just discovered the concept of an auto_ptr and am liking it! As Qt often requires a QList or QVector<(some QObject or QWidget) *>, is there any concrete reason why auto_ptr should be avoided. If I'm right, it allows you to replace this:
std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size_t i=0; i<vec.length(); ++i)
{
delete vec[i];
}
vec.clear();
with somthing much shorter (ie no cleanup)
std::vector<auto_ptr<MyClass>> vec;
/* add several elements to the vector and do stuff with them */
// no need for the delete loop
...Can Qt still work its shared memory magic with auto_ptr's? Does the parent-child automagic memory management still function transparently? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Qt 有自己的智能指针类,它们在很多方面都优于 std::auto_ptr,特别是其中一些可以毫无问题地放入容器中。
std::auto_ptr
具有所有权转移复制语义,因此如果您尝试将其放入容器中,将无法按预期工作。尝试使用
QSharedPointer
。它是一个引用计数智能指针,当智能指针不再有副本时,它会删除其包含的对象。与 std::auto_ptr 不同,同一个 QSharedPointer 可以同时有多个副本,因此它可以很好地与容器配合使用。Qt has its own smart pointer classes that are in many ways superior to
std::auto_ptr
, specifically in that some of them can be put into containers with no issues.std::auto_ptr
has ownership-transfer copy semantics, and so will not work as expected if you try to put it into a container.Try using
QSharedPointer
. It's a reference counted smart pointer that deletes its contained object when there are no more copies of the smart pointer left. Unlikestd::auto_ptr
there can be multiple copies of the sameQSharedPointer
at once, and therefore it plays well with containers.如果您想要一个拥有指针所有权的容器,那么请查看 boost 指针容器。
您将指针放入容器中,但与其他容器一样,它们会被视为普通对象,这使得更容易通过标准算法使用它们(即无需编写包装类)。当指针容器超出范围时,它将对容器中的所有指针调用删除:
If you want a container that has ownership of pointers then look at boost pointer containers.
You put pointers into the container, but like the other containers they are then treated like normal objects which makes it easier to use them with standard algorithms (i.e. no need to write wrapper classes). When the pointer container goes out of scope it will call delete on all pointers in the container:
std::auto_ptr
不能在std::vector
中使用,因为std::vector
期望能够复制其内容,并且您无法在正常意义上复制std::auto_ptr
。复制意味着最终会得到两个相同的东西,如果你有两个相同的 std::auto_ptr ,那么当它们超出范围时,它们所指向的内容将被双重释放。 (所发生的情况是,被复制的auto_ptr
的内部指针被清零,而被复制到的指针现在是旧的指针。)使用
shared_ptr
,通常可作为boost::shared_ptr
或在 Visual C++std::tr1::shared_ptr
上使用,并且将位于 C++0x 标准库中,或者使用 Qt 拥有的任何东西。它们可以被复制,因此可以放入容器中。std::auto_ptr
cannot be used instd::vector
, becausestd::vector
expects to be able to copy its contents, and you can't copy astd::auto_ptr
in the normal sense. Copying means winding up with two identical things, and if you had two identicalstd::auto_ptr
s what they pointed to would be double-freed when they went out of scope. (What happens instead is that theauto_ptr
being copied from has its internal pointer zeroed out, and the one being copied to is now what the old one used to be.)Use
shared_ptr
, which is often available asboost::shared_ptr
or on Visual C++std::tr1::shared_ptr
, and which will be in the C++0x standard library, or use whatever Qt has. Those can be copied, and therefore can go into containers.