make_shared (<memory>)

Cria e retorna shared_ptr apontando para objetos que são atribuídos construídos de zero ou mais argumentos usando o distribuidor padrão.

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

Parâmetros

Parâmetro

Descrição

_Args

Argumentos de construtor.A função infere que a sobrecarga de construtor para chamar com base nos argumentos que são fornecidos.

Valor de Propriedade/Valor Retornado

Retorna shared_ptr que aponta para o objeto atribuído.

Comentários

A função cria o objeto shared_ptr<Type>, um ponteiro para Type(_Args...) como atribuído e construído pelo distribuidor padrão allocator().O exemplo a seguir mostra como criar compartilhados ponteiros para um tipo invocando sobrecargas específicas do construtor.

Exemplo

#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

Cabeçalho: <memory>

Namespace: STD

Consulte também

Referência

<memory>

shared_ptr Class