Move Semantices and Unique pointers

take-away points:

  1. for some stl container, if passing by rvalue, it will call move constructor.
    vector v;
    v.push_back(string("hello"));   // pass rvalue, call move constructor
    string s("hello");
    v.push_back(move(s));           // element of v takes ownership of v and set s to be null. 
    
  2. unique pointers and shared pointers.
    It’s encouraged to use smart pointers, when doing memory allocation. so we don’t need to worry about memory leak when it goes out of scope.
  3. unique ptr: exclusive ownership; shared ptr: shared ownership.
  4. for shared ptr: delete happens when last ptr that owns the object goes out of scope

Source: Lesson #5: Move Semantics

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s