Condividi tramite


make_shared (<memory>)

Crea e restituisce shared_ptr che indica gli oggetti allocati che vengono costruite da zero o più argomenti utilizzando l'allocatore predefinito.

template<class Type, class... Types>
    shared_ptr<Type> make_shared(
        Types&&... _Args
    );

Parametri

Parametro

Descrizione

_Args

Argomenti del costruttore.La funzione tramite l'overload del costruttore richiamare in base agli argomenti forniti.

Valore proprietà/Valore restituito

Restituisce shared_ptr che punti all'oggetto allocato.

Note

La funzione crea l'oggetto shared_ptr<Type>, un puntatore a Type(_Args...) come allocato e costruito da un allocatore predefinito allocator().Di seguito viene illustrato come creare condiviso i puntatori a un tipo chiamando gli overload del costruttore specifici.

Esempio

#include <iostream>
#include <string>
#include <memory>

using namespace std;

class Zebra
{
private:
    int nStripes;
    string name;
public:
    Zebra() : nStripes(-1), name("Default")
    {
        cout << "I'm a default Zebra." << endl;
    }

    Zebra(int i, string s) : nStripes(i), name(s)
    {
        
        std::cout << "My name is " << name 
                  << " and I have " << nStripes 
                  << " stripes." << endl;
    }

    virtual ~Zebra()
    {
        cout << "Goodbye from " << name << endl;


    }

};

void MakeZebras()
{
    auto pzeb = make_shared<Zebra>();
    auto pGeorge = make_shared<Zebra>(5, "George");
}



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(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;
    }


};

shared_ptr<Song> MakeSongPtr(wstring artist, wstring title)
{
    Song* s = new Song(artist, title);
    shared_ptr<Song> p(s);
    //return make_shared<Song>(artist,title);
    return p;
}

Requisiti

Intestazione: <memory>

Spazio dei nomi: deviazione standard

Vedere anche

Riferimenti

<memory>

shared_ptr Class