make_shared (<memory>)
Visual Studio 2012
Creates and returns a shared_ptr that points to the allocated objects that are constructed from zero or more arguments by using the default allocator.
template<class Type, class... Types>
shared_ptr<Type> make_shared(
Types&&... _Args
);
#include <iostream> #include <string> #include <memory> using namespace std; class SongBase { protected: std::wstring id; public: SongBase() : id(L"default"){} SongBase(std::wstring init) : id(init) {} virtual ~SongBase(){} }; class Song : public SongBase { public: std::wstring title_; std::wstring artist_; std::wstring duration_; std::wstring format_; Song(std::wstring title, std::wstring artist) : title_(title), artist_(artist){} Song(Song&& other) { title_ = other.title_; artist_ = other.artist_; duration_ = other.duration_; format_ = other.format_; other.title_ = nullptr; other.artist_ = nullptr; other.duration_ = nullptr; other.format_ = nullptr; } ~Song() { std::wcout << L"deleting " << title_ << L":" << artist_ << std::endl; } Song& operator=(Song&& other) { if(this != &other) { this->artist_ = other.artist_; this->title_ = other.title_; this->duration_ = other.duration_; this->format_ = other.format_; other.artist_ = nullptr; other.title_ = nullptr; other.duration_ = nullptr; other.format_ = nullptr; } return *this; } bool operator ==(const Song& other) { return this->artist_.compare(other.artist_) == 0 && this->title_.compare(other.title_) == 0; } }; // we would need this helper function if we didn't have // make_shared<Song>(artist, title) available. shared_ptr<Song> MakeSongPtr(wstring artist, wstring title) { Song* s = new Song(artist, title); shared_ptr<Song> p(s); return p; }