Jump to content

  • Log In with Google      Sign In   
  • Create Account


unique_ptr etc


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Quat   Members   -  Reputation: 401

Like
0Likes
Like

Posted 11 December 2012 - 01:17 PM

I'm trying to start integrating the new C++11 pointer types.

My understanding is:

1. Use unique_ptr for the pointer that is responsible for managing the object's life.
2. Use shared_ptr for a reference count like system.

If you just want a reference to an object, do you just use a "regular pointer" ?
-----Quat

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7774

Like
0Likes
Like

Posted 11 December 2012 - 01:30 PM

Preferably you would use a reference, but a normal pointer would work also, if you just want to refer to an existing object and not claim or affect its ownership status in any way.

#3 SiCrane   Moderators   -  Reputation: 9387

Like
0Likes
Like

Posted 11 December 2012 - 01:37 PM

If that object is owned by a shared_ptr you can use a weak_ptr.

#4 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 12 December 2012 - 09:29 PM

In my code,

1, I use unique_ptr when possible if no need to share ownership. Then I pass raw pointer everywhere. When a function sees a raw pointer, it knows it should not free the pointer.
2, I use shared pointer when I have to share the ownership, but I would avoid it if possible. For shared pointer, I usually pass raw pointer to other functions too, unless the calling function will retain the ownership. Also, if an object doesn't need strong ownership, I use weak pointer when possible. I prefer weak pointer over shared pointer to avoid cycle reference.
3, I rarely use "delete" explicitly thanking to above two rules.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#5 sednihp   Members   -  Reputation: 241

Like
0Likes
Like

Posted 15 December 2012 - 03:40 PM

There's some good reading on smart pointers and example code here:

Smart pointers

That page has links to unique_ptr, shared_ptr and weak_ptr specific pages as well, all with examples.

Edited by sednihp, 15 December 2012 - 03:40 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS