If you pass a smart pointer as a parameter to a function and you use in this function only the underlying resource of the smart pointer, you do something wrong. In this case, you should use a pointer or a reference as a function parameter, because you don't the lifetime semantic of a smart pointer.
The reference counter in line 1 is 2 because I used the shared pointer firSha to copy-initialised secSha. Let's have a closer look at the invocation of the function asSmartPointerGood 2.
First 3 , the reference count of shr is 2 and then, it becomes 1 in the line 4. What happened in line 5? I reset shr to the new resource: new int Consequentially, both the shared pointer firSha and s ecSha are immediately shared owner of different resources.
You can observe the behaviour in the screenshot. If you invoke reset on a shared pointer, magic happens under the hood. This magic is not necessary if you only interested in the underlying resource of the shared pointer; therefore, a pointer or a reference is the right kind of parameter for the function asSmartPointerBad 6. Have a look also at a recent post by Bartek F.
There are six rules left for passing smart pointers to functions. So you know, what I will write about in my next post. Download Microsoft Edge More info.
Contents Exit focus mode. Is this page helpful? Yes No. Any additional feedback? Skip Submit. Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today. It is based on exclusive ownership model i. As shown in the below program, copying or assigning of pointers changes the ownership i. It takes ownership of the pointer in a way that no two pointers should contain the same object.
Assignment transfers ownership and resets the rvalue auto pointer to a null pointer. It is a container for raw pointers. In your precise case: why not return a reference? To achieve encapsulation: You shall not return handles to your internals see Law of Demeter. Matthieu M. Is your note a subtle joke? If so, it is too subtle for me, if not, then it is the same bug as with a raw pointer except for the double delete or memory leak with raw pointers — stefaanv.
Matthieu, okay, then the note is not really clear to me. I'll try to clarify the note. Show 1 more comment. If ownership is an issue, use a smart pointer. If not, I'd use a reference if practicable.
You allocate B at constuction of A. You say B shouldn't persist outside As lifetime. Both these point to B being a member of A and a just returning a reference accessor. Are you overengineering this? Ricibob Ricibob 7, 3 3 gold badges 42 42 silver badges 61 61 bronze badges. It might make them think about it, though, and of course if they happen not to have it locked when A is destroyed then B can go too. When you say: "Let's say B is a class that semantically should not exist outside of the lifetime of A" This tells me B should logically not exist without A, but what about physically existing?
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog.
0コメント