Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualdeathwearer

Posted 11 December 2012 - 04:56 PM

I get the concept (Or I think I do) but I still have couple of question on the technical side of unique_ptr.

1. When you declare a unique_ptr as a class variable, you don't have to delete it in the destructor because it is not on the heap so when the class instance is deleted, the unique_ptr is deleted which trigger the real pointer delete?

2. How do you pass unique_ptr to other functions. Do you pass a ref to the unique_ptr object? Or do you pass uniqueptr.get() (Which sends the pointer to the real object)? Do you do the same if you send it to an internal method than to another class method? I personally pass the pointer as const, leaving the unique_ptr hidden. I'm wondering here what are the best practices.

3. What is the point of a unique_ptr as a class variable? I mean since the pointer has to dies when the owning instance is kill, all you have to do is to put it in the destructor. I understand that shared_ptr are more complicated since many object can own them which make the delete call tricky, but in the case of the unique_ptr, it's pretty straightforward.

4. What are the best practices regarding unique_ptr and passing the pointer to the object is hold to another class which stores it in a class variable. Is that a good practice first? The unique_ptr could be deleted and the pointer copy it hold in the other class would be invalid. Are unique_ptr meant to be pass around like that? Should i use shared_ptr in that case we're talking? So if another class stores it in a class variable the pointer would be deleted when only all the instances of these 2 classes would be deleted. Also how can you predict such cases if the good answer is to use shared_ptr. How can I plan that other people that might use my class could store my pointer internally.

#1deathwearer

Posted 11 December 2012 - 03:53 PM

I get the concept (Or I think I do) but I still have couple of question on the technical side of unique_ptr.

1. When you declare a unique_ptr as a class variable, you don't have to delete it in the destructor because it is not on the heap so when the class instance is deleted, the unique_ptr is deleted which trigger the real pointer delete?

2. How do you pass unique_ptr to other functions. Do you pass a ref to the unique_ptr object? Or do you pass uniqueptr.get() (Which sends the pointer to the real object)? Do you do the same if you send it to an internal method than to another class method? I personally pass the pointer as const, leaving the unique_ptr hidden. I'm wondering here what are the best practices.

3. What is the point of a unique_ptr as a class variable? I mean since the pointer has to dies when the owning instance is kill, all you have to do is to put it in the destructor. I understand that shared_ptr are more complicated since many object can own them which make the delete call tricky, but in the case of the unique_ptr, it's pretty straightforward.

PARTNERS