sundeepblue
4/5/2014 - 9:51 PM

the usage of unique_ptr, smart pointer

the usage of unique_ptr, smart pointer

void use_raw_pointer() {
  Song *pSong = new Song("My heart will go on");
  
  // use pSong ...
  
  delete pSong;
}

void use_smart_pointer() {
  unique_ptr<Song> song1(new Song("My heart will go on"));
  
  // use song1 ...
} // song1 is deleted automatically here


// The following demo shows how to create unique_ptr instances and use them in a vector
void SongVector() {
  vector<unique_ptr<Song>> songs;
  // create some new unique_ptr<Song> instances, add them into vector
  songs.push_back(make_unique<Song>("song title 1"));
  songs.push_back(make_unique<Song>("song title 2"));
  
  // pass by const reference when possible to avoid copying
  for(const auto& song : songs)
    cout << song->title << endl;
}