Scoped_ptr owner

Possible duplicate:
What is a smart pointer and when to use it?

I read the article and I found a small example demonstrating the use of boost::scoped_ptr<T> :

 #include <cstdlib> #include <iostream> #include <boost/scoped_ptr.hpp> #include <boost/scoped_array.hpp> static int count = 0; class printer { int m_id; public: printer(void) : m_id(count++) { } ~printer(void) { std::cout << "Printer " << m_id << " destroyed" << std::endl; } }; int main(void) { boost::scoped_ptr<printer> p1(new printer); boost::scoped_ptr<printer> p2(new printer); std::cout << "Exiting test program" << std::endl; return EXIT_SUCCESS; } 

The only thing that I did not understand in the article is the statement:

using scoped_ptr , you indicate that the transfer of ownership is not intended or permitted.

Perhaps this was the wrong article starting with newcomers to this thread, but what does the above line mean?

+7
source share
1 answer

Most smart pointers have ownership of the object they are pointing to โ€” they are responsible for destroying that object when the time comes. However, different smart pointers have different semantics of ownership. That is, they tell the user of this smart pointer how ownership can or cannot be transferred, how it is distributed between objects, when an object should be deleted, and so on. Using a specific smart pointer describes your intention regarding ownership of this property. Property can be transferred to other functions or objects.

boost::scoped_ptr has very strict property semantics. He does not allow the transfer of property rights at all. It achieves this by not being copied (so you cannot pass it to another function by value).

As another example, a std::unique_ptr also pretty strict. Its special ability is that it can be moved. Passing the function std::unique_ptr function as rvalue will allow this function to steal ownership of the object. The original std::unique_ptr loses this property immediately. This ensures that ownership is forever held by only one std::unique_ptr .

The equivalent of boost::scoped_ptr in C ++ 11 is equal to const std::unique_ptr . This is due to the fact that making it const prevents any movement and, therefore, ownership cannot be transferred.

An easy way to see how important the semantics of rights is with an example. Let's say you use an evil developer library and it has a function that you do not know about the implementation, for example:

 cat* foo(); 

You know that this function returns a pointer to cat . However, this is a raw pointer. You do not know whether at some point you should destroy the cat (using delete ), or if the library does it for you. You donโ€™t even know if the object was really dynamically distributed. You donโ€™t even suspect that the library is still holding cat . In the past, if you had such a feature, you would need to find documentation to find out what to do. However, now that we have smart pointers in addition to the original pointers, raw pointers have their own semantics - the most relaxed of all. It says: "You better trust me that I keep this cat valid as long as you pass it, but I will be the one who controls it. Do not hold it for too long."

However, a wise and kind library developer will now write this function as follows:

 std::unique_ptr<cat> foo(); 

How does this help? Well, std::unique_ptr tells you a lot. It tells you that the function waives ownership of the cat object for you. cat now your sole responsibility. It is also very useful for giving you a smart pointer, because you do not need to think about delete about it. You can simply use the pointer, and when it goes out of scope, the object will be destroyed. Or, if you want, you can transfer ownership of another function.

This does not mean that only one pointer will ever own cat . As a proud new owner, you decide what will happen next. Itโ€™s completely wise for you to decide that you want to start sharing your cat :

 std::unique_ptr<cat> up = foo(); std::shared_ptr<cat> sp(up.release()); 

The wise and kind foo library developer told you what her intentions were. She gave you a cat , and now you are the owner. Now you can provide your own property semantics.

So boost::scoped_ptr bit like a greedy cat hoarder that will never share a cat with anyone, never give the cat to anyone and keep it until they die.

+15
source

All Articles