I am in the process of learning C++11 features and as part of that I am diving head first into the world of unique_ptr
and shared_ptr
.
When I started, I wrote some code that used unique_ptr
exclusively, and as such when I was passing my variables around I needed to accomplish that with std::move
(or so I was made to understand).
I realized after some effort that I really needed shared_ptr
instead for what I was doing. A quick find/replace later and my pointers were switched over to shared but I lazily just left the move()
calls in.
To my surprise, not only did this compile, but it behaved perfectly well in my program and I got every ounce of functionality I was expecting... particularly, I was able to "move" a shared_ptr
from ObjectA to ObjectB, and both objects had access to it and could manipulate it. Fantastic.
This raised the question for me though... is the move()
call actually doing anything at all now that I am on shared_ptr
? And if so, what, and what are the ramifications of it?
Code Example
shared_ptr<Label> lblLevel(new Label());
//levelTest is shared_ptr<Label> declared in the interface of my class, undefined to this point
levelTest = lblLevel;
//Configure my label with some redacted code
//Pass the label off to a container which stores the shared_ptr in an std::list
//That std::list is iterated through in the render phase, rendering text to screen
this->guiView.AddSubview(move(lblLevel));
At this point, I can make important changes to levelTest like changing the text, and those changes are reflected on screen.
This to me makes it appear as though both levelTest
and the shared_ptr in the list are the same pointer, and move()
really hasn't done much. This is my amateur interpretation. Looking for insight. Using MinGW on Windows.