make_shared (<memory>)

Crea y devuelve shared_ptr que señala a los objetos asignados que se construyen a partir de cero o más argumento utilizando el asignador predeterminado.

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

Parámetros

Parámetro

Descripción

_Args

Argumentos del constructor.La función deduce que la sobrecarga de constructor a invocar según los argumentos que se proporcionan.

Valor de propiedad y valor devuelto

Devuelve shared_ptr del objeto asignado.

Comentarios

La función crea el objeto shared_ptr<Type>, un puntero a Type(_Args...) como asignado y construido enlazando el asignador predeterminado allocator().El ejemplo siguiente se muestra cómo crear punteros compartidos a un tipo invocando sobrecargas del constructor.

Ejemplo

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

Requisitos

Encabezado: <memory>

Espacio de nombres: std

Vea también

Referencia

<memory>

shared_ptr Class