Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use std::unique_ptr and std::shard_ptr
Forum Updated to NodeBB v4.3 + New Features

How to use std::unique_ptr and std::shard_ptr

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 8.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    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.

    (Z(:^

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Diluka
      wrote on last edited by
      #3

      Thanks for reply :)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Diluka
        wrote on last edited by
        #4

        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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #5

          Hi,

          std::unique_ptr and std::shared_ptr are definex in C++11 standard; to use them you should enable this standard in your compiler.
          If you're using GCC you can use -std=c++11 flag

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PeerS
            wrote on last edited by
            #6

            One thing to remember here is that auto_ptr is deprecated in C++11, use unique_ptr instead.
            Peer

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              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

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • P Offline
                P Offline
                PeerS
                wrote on last edited by
                #8

                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.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #9

                  [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 ;-)

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    PeerS
                    wrote on last edited by
                    #10

                    [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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #11

                      [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. :-)

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        Diluka
                        wrote on last edited by
                        #12

                        Thanks all of you , learn lot of things :)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved