Blogitter

Smart pointer declaration

append delete Code Maniac
%
C++
void UseRawPointer()
{
    // Using a raw pointer -- not recommended.
    Song* pSong = new Song(L"Nothing on You", L"Bruno Mars"); 

    // Use pSong... 

    // Don't forget to delete! 
    delete pSong;   
}


void UseSmartPointer()
{
    // Declare a smart pointer on stack and pass it the raw pointer.
    unique_ptr<Song> song2(new Song(L"Nothing on You", L"Bruno Mars"));

    // Use song2...
    wstring s = song2->duration_;
    //...

} // song2 is deleted automatically here.
%

Reply RSS

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Use markup to add links, quotes, bold, italic and more. You can also upload images or videos or files or archived web pages or multicontent or use any upload website.

Moderators: Blogitter Team

  1. Home
  2. » Pastebin

ABOUT :: BLOGITTER NEWS :: FEEDBACK :: MULTILINGUAL :: FORMAT TEXT :: RSS FEEDS :: REQUEST NEW CATEGORY :: REPORT ABUSE :: HELP :: TERMS OF USE :: CONTACT