Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Erstellt und gibt shared_ptr zurück, das an den zugeordneten Objekten, die von keinem erstellt oder mehr Argumenten zeigt, indem die standardmäßige Belegungsfunktion verwendet.
template<class Type, class... Types>
shared_ptr<Type> make_shared(
Types&&... _Args
);
Parameter
Parameter |
Beschreibung |
|---|---|
_Args |
Konstruktorargumente.Die Funktion leitet ab, die auf der Argumente aufzurufen, die Konstruktorüberladung, die bereitgestellt werden. |
Eigenschaftswert/Rückgabewert
Gibt shared_ptr zurück, das dem - Objekt zugeordneten Punkte.
Hinweise
Die Funktion erstellt das Objekt shared_ptr<Type>, ein Zeiger auf Type(_Args...), wie zugeordnet und von den Belegungsfunktion allocator() erstellt.Im folgenden Beispiel wird gezeigt, wie freigegebene Zeiger auf einen Typ durch das Aufrufen von bestimmten Konstruktorüberladungen erstellt wird.
Beispiel
#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;
}
Anforderungen
Header: <memory>
Namespace: std