How to use std::unique_ptr and std::shard_ptr
-
Hi
Have a nice day , I like to learn std::auto_ptr and std::shard_ptr please anyone have better idea about that pleae share your ideas ,because its really help me to learn it quickely specially i like to know how they used in practical environment and what is the difference between these two items.
Thanks
[[merged two very similar posts by the same user, Tobias]]
-
This has little to do with Qt itself - wrong subforum :)
To answer your question:
[quote]Therefore, no two auto_ptr objects should own the same element, since both would try to destruct them at some point. When an assignment operation takes place between two auto_ptr objects, ownership is transferred, which means that the object losing ownership is set to no longer point to the element (it is set to the null pointer).[/quote]While shared pointer's philosophy is completely different:
[quote]std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object.[/quote]Sources: "auto_ptr":http://www.cplusplus.com/reference/memory/auto_ptr/, "shared_ptr":http://en.cppreference.com/w/cpp/memory/shared_ptr.
-
Hi
Have a nice day .I need to know std::unique_ptr and std::shard_ptr ,I tryed to use that two items inside the Qt creater but if i used std:: its not genarate these two items and i also used #include<memory> but still i can't use these two items please anyone have working examples please share with me :)
Thanks
-
there are also Qt flavors for it.
"QScopedPointer":http://qt-project.org/doc/qt-5.0/qtcore/qscopedpointer.html
"QSharedPointer":http://qt-project.org/doc/qt-5.0/qtcore/qsharedpointer.html -
For projects not using C++11 QScopedPointer, QSharedPointer and QWeakPointer are great, however for projects using C++11 I would use std::unique_ptr, std::shared_ptr and std::weak_ptr.
A big plus IMHO is also that std::unique_ptr can be used in containers while you can not do this with QScopedPointer.
-
[quote author="PeerS" date="1367849422"]For projects not using C++11 QScopedPointer, QSharedPointer and QWeakPointer are great, however for projects using C++11 I would use std::unique_ptr, std::shared_ptr and std::weak_ptr.
A big plus IMHO is also that std::unique_ptr can be used in containers while you can not do this with QScopedPointer.[/quote]
Agreed
Actually, I have been using boost's shared_ptr for a long time. So, it is not limited to C++11. However, boost's docs are not great when you start with. Qt's docs are much better. ... and since this is a Qt forum, I thought we should notice at least ;-) -
[quote author="koahnig" date="1367851801"]
... and since this is a Qt forum, I thought we should notice at least ;-)
[/quote]They are absolutely worth to be mentioned :)
To be honest, before reading your post I did not even know about the Qt smart pointers even though I use Qt since several years now.
Peer -
[quote author="PeerS" date="1367852418"]
They are absolutely worth to be mentioned :)
To be honest, before reading your post I did not even know about the Qt smart pointers even though I use Qt since several years now.
Peer
[/quote]Somehow this sounds familiar. I have learnt about those pointers also through this forum. :-)