Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to use new C++11 unique and shared pointers?  (Read 3165 times)

0 Members and 1 Guest are viewing this topic.

firefly2442

  • Newbie
  • *
  • Posts: 23
    • View Profile
How to use new C++11 unique and shared pointers?
« on: October 10, 2015, 09:49:24 pm »
I'm trying to update my game object manager to not just a straight pointer but to use new C++11 shared and unique pointers.  I think I need to use a shared_ptr because I want to be able to get access to the objects and change them outside of the manager class.  This code is based off of an older tutorial and I'm trying to make adjustments based on comments by the author about pointers.  I'm also confused about why the Get() method is const.  If I want to make changes to the objects inside the manager, do I need to remove this?

Satus

  • Guest
Re: How to use new C++11 unique and shared pointers?
« Reply #1 on: October 10, 2015, 11:19:54 pm »
Quote
I think I need to use a shared_ptr because I want to be able to get access to the objects and change them outside of the manager class.

No, you don't need shared pointers to be able to get access and change objects outside of class. Unique pointer is what you want, because your manager is (probably) the one and only owner of the objects.
There are tons of tutorials, really, google it.

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: How to use new C++11 unique and shared pointers?
« Reply #2 on: October 11, 2015, 02:30:24 am »
You can still change the pointed-to object despite get() being const. Just try it.

    const std::unique_ptr<int> ptr{new int(10)};
    std::cout << *ptr << std::endl; // outputs 10
    *ptr.get() = 20;
    std::cout << *ptr << std::endl; // outputs 20

Maybe your confusion is because using unique_ptr changes the type of the object. Instead of dealing with an object of type T, or the address of T, you are now instead dealing with an object of type std::unique_ptr<T>.
« Last Edit: October 11, 2015, 02:36:46 am by Mörkö »

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: How to use new C++11 unique and shared pointers?
« Reply #3 on: October 11, 2015, 03:20:26 am »
Smart pointers are used to manage the lifetime of an object allocated on the heap, not necessarily as a wrapper for all pointers.

unique_ptr is to prevent having to call delete on the pointer, but instead have it done automatically once you leave the scope, thus preventing accidental memory leaks and making the code much cleaner.

shared_ptr is used for having delete called on the pointer as soon as you drop all "references" (shared_ptr's) to the object, much like how objects behave in languages such as Java or C# (just be careful when extracting the raw pointer from a shared_ptr, as they do count as a "reference", that and cyclic references may be a source of memory leaks).

I didn't read the code, but it sounds like unique_ptr is what you probably need. Remember that shared_ptr has a small, but existent runtime overhead.

Relevant stuff I found by using google:
https://msdn.microsoft.com/en-us/library/hh279674.aspx
http://www.umich.edu/~eecs381/handouts/C++11_smart_ptrs.pdf
http://www.informit.com/articles/article.aspx?p=2085179
https://meetingcpp.com/index.php/br/items/an-overview-on-smart-pointers.html

Please use a C++ forum next time.
« Last Edit: October 11, 2015, 03:25:50 am by GraphicsWhale »