std::shared_ptr::shared_ptr
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<metanoindex/>
<tbody> </tbody> constexpr shared_ptr(); |
(1) | |
template< class Y > explicit shared_ptr( Y* ptr ); |
(2) | |
template< class Y, class Deleter > shared_ptr( Y* ptr, Deleter d ); |
(3) | |
template< class Y, class Deleter, class Alloc > shared_ptr( Y* ptr, Deleter d, Alloc alloc ); |
(4) | |
constexpr shared_ptr( std::nullptr_t ); |
(5) | |
template< class Deleter > shared_ptr( std::nullptr_t, Deleter d ); |
(6) | |
template< class Deleter, class Alloc > shared_ptr( std::nullptr_t, Deleter d, Alloc alloc ); |
(7) | |
template< class Y > shared_ptr( const shared_ptr<Y>& r, T *ptr ); |
(8) | |
shared_ptr( const shared_ptr& r ); |
(9) | |
template< class Y > shared_ptr( const shared_ptr<Y>& r ); |
(9) | |
shared_ptr( shared_ptr&& r ); |
(10) | |
template< class Y > shared_ptr( shared_ptr<Y>&& r ); |
(10) | |
template< class Y > explicit shared_ptr( const std::weak_ptr<Y>& r ); |
(11) | |
template< class Y > shared_ptr( std::auto_ptr<Y>&& r ); |
(12) | |
template< class Y, class Deleter > shared_ptr( std::unique_ptr<Y,Deleter>&& r ); |
(13) | |
Costruisce
shared_ptr nuovo da una varietà di tipi di puntatore che fanno riferimento ad un oggetto da gestire. Opzionale deleter d possono essere forniti, che viene poi utilizzato per distruggere l'oggetto quando nessun oggetto shared_ptr lo possiede. Per impostazione predefinita, l'espressione è usata come eliminare espressione deleter. Espressione delete corretta corrispondente al tipo in dotazione è sempre selezionato, questo è il motivo per cui sono implementati i costruttori come modelli che utilizzano un parametro Y separata.Original:
Constructs new
shared_ptr from a variety of pointer types that refer to an object to manage. Optional deleter d can be supplied, which is later used to destroy the object when no shared_ptr objects own it. By default, eliminare espressione expression is used as deleter. Proper delete expression corresponding to the supplied type is always selected, this is the reason why the constructors are implemented as templates using a separate parameter Y.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Se l'oggetto puntato da
ptr è già di proprietà di qualche puntatore condiviso, i risultati del costruttore in un comportamento indefinito.Original:
If the object pointed to by
ptr is already owned by some shared pointer, the constructor results in undefined behavior.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Costruttore di default costruisce un
shared_ptr senza oggetto gestito, shared_ptr cioè vuotoOriginal:
Default constructor constructs a
shared_ptr with no managed object, i.e. empty shared_ptrThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2-4)
Costruisce un
shared_ptr con ptr come oggetto gestito. Y deve essere di tipo completo e convertibile in modo implicito T. Inoltre:Original:
Constructs a
shared_ptr with ptr as the managed object. Y must be a complete type and implicitly convertible to T. Additionally:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Utilizza l'espressione di eliminazione come il deleter. Un'espressione di cancellazione valida deve essere disponibile,
delete ptr cioè devono essere ben formati, hanno un comportamento ben definito e non gettare alcuna eccezione.Original:
Uses the delete expression as the deleter. A valid delete expression must be available, i.e.
delete ptr must be well formed, have well-defined behavior and not throw any exceptions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Utilizza la
d specificato deleter come deleter. Deleter deve essere richiamabile per il tipo di T, cioè d(ptr) devono essere ben formati, hanno un comportamento ben definito e non generare alcuna eccezione. Deleter deve essere CopyConstructible.Original:
Uses the specified deleter
d as the deleter. Deleter must be callable for the type T, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter must be CopyConstructible.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
Stessa (3), ma utilizza inoltre una copia di
alloc per l'assegnazione dei dati per uso interno. Alloc deve essere un Allocator. Il costruttore di copia e distruttore non devono generare eccezioni.Original:
Same as (3), but additionally uses a copy of
alloc for allocation of data for internal use. Alloc must be a Allocator. The copy constructor and destructor must not throw exceptions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5-7)
Analoga alla (2), (3), (4), rispettivamente, ma costruisce un
shared_ptr senza oggetto gestito, cioè vuoto shared_ptr.Original:
Analogous to (2), (3), (4) respectively, but constructs a
shared_ptr with no managed object, i.e. empty shared_ptr.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8)
Costruisce un
shared_ptr che condivide informazioni di proprietà con r, ma tiene un puntatore ptr indipendente e non gestiti. Anche se questo shared_ptr è l'ultimo del gruppo di uscire di portata, si chiamerà il distruttore per l'oggetto originariamente gestito da r. Tuttavia, chiamando get() su questo restituisce sempre una copia di ptr. E 'responsabilità del programmatore per fare in modo che questo ptr rimane valido fino a quando questo shared_ptr esiste, come ad esempio nei casi d'uso tipici dove ptr è un membro dell'oggetto gestito da r o un alias (ad esempio, bassi) di r.get()Original:
Constructs a
shared_ptr which shares ownership information with r, but holds an unrelated and unmanaged pointer ptr. Even if this shared_ptr is the last of the group to go out of scope, it will call the destructor for the object originally managed by r. However, calling get() on this will always return a copy of ptr. It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by r or is an alias (e.g., downcast) of r.get()The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
9)
Costruisce un
shared_ptr che condivide la proprietà dell'oggetto gestito da r. Se r gestisce nessun oggetto, *this gestisce nessun oggetto troppo. Il sovraccarico basato su modelli non prende parte alla risoluzione di sovraccarico se Y* non è convertibile in modo implicito T*.Original:
Constructs a
shared_ptr which shares ownership of the object managed by r. If r manages no object, *this manages no object too. The templated overload doesn't participate in the overload resolution if Y* is not implicitly convertible to T*.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
10)
Move-costruisce una
shared_ptr da r. Dopo la costruzione, *this contiene una copia del precedente stato di r, r è vuoto. Il sovraccarico basato su modelli non prende parte alla risoluzione di sovraccarico se Y* non è convertibile in modo implicito T*.Original:
Move-constructs a
shared_ptr from r. After the construction, *this contains a copy of the previous state of r, r is empty. The templated overload doesn't participate in the overload resolution if Y* is not implicitly convertible to T*.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
11)
Costruisce un
shared_ptr che condivide la proprietà dell'oggetto gestito da r. Y* deve essere convertibile in T*. Si noti che r.lock() possono essere utilizzati per lo stesso scopo: la differenza è che questo costruttore genera un'eccezione se l'argomento è vuoto, mentre std::weak_ptr<T>::lock() costruisce un std::shared_ptr vuoto in quel caso.Original:
Constructs a
shared_ptr which shares ownership of the object managed by r. Y* must be convertible to T*. Note that r.lock() may be used for the same purpose: the difference is that this constructor throws an exception if the argument is empty, while std::weak_ptr<T>::lock() constructs an empty std::shared_ptr in that case.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
12)
Costruisce un
shared_ptr che memorizza e possiede l'oggetto in precedenza di proprietà di r. Y* deve essere convertibile in T*. Dopo la costruzione, r è vuoto.Original:
Constructs a
shared_ptr that stores and owns the object formerly owned by r. Y* must be convertible to T*. After construction, r is empty.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
13)
Costruisce un
shared_ptr che gestisce l'oggetto attualmente gestita da r. Il deleter associata r viene memorizzato per l'eliminazione futuro l'oggetto gestito. r gestisce nessun oggetto dopo la chiamata. Y* deve essere convertibile in T*.Original:
Constructs a
shared_ptr which manages the object currently managed by r. The deleter associated to r is stored for future deletion of the managed object. r manages no object after the call. Y* must be convertible to T*.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Note
Quando si costruisce da un puntatore a un oggetto prima di un tipo derivato da std::enable_shared_from_this, i costruttori di
shared_ptr rilevare altri proprietari dell'oggetto attraverso il riferimento debole memorizzato in quella classe di base e gli assetti proprietari con loro, invece di assumere che l'oggetto non è gestito.Original:
When constructing from a raw pointer to an object of a type derived from std::enable_shared_from_this, the constructors of
shared_ptr detect other owners of the object through the weak reference stored in that base class and share ownership with them, instead of assuming that the object is not managed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Parametri
| ptr | - | un puntatore ad un oggetto da gestire
Original: a pointer to an object to manage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| d | - | deleter una da utilizzare per distruggere l'oggetto
Original: a deleter to use to destroy the object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| alloc | - | un allocatore da utilizzare per la ripartizione dei dati per uso interno
Original: an allocator to use for allocations of data for internal use The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| r | - | un altro puntatore intelligente per condividere la proprietà o acquisire la proprietà di
Original: another smart pointer to share the ownership to or acquire the ownership from The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Eccezioni
1)
2)
std::bad_alloc se richiesto ulteriore memoria non può essere ottenuto. Può lanciare implementazione definita un'eccezione per altri errori.
delete ptr viene chiamato se si verifica un'eccezione.Original:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors.
delete ptr is called if an exception occurs.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3-4)
std::bad_alloc se richiesto ulteriore memoria non può essere ottenuto. Può lanciare implementazione definita un'eccezione per altri errori.
d(ptr) viene chiamato se si verifica un'eccezione.Original:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors.
d(ptr) is called if an exception occurs.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5)
6-7)
std::bad_alloc se richiesto ulteriore memoria non può essere ottenuto. Può lanciare implementazione definita un'eccezione per altri errori. Se viene generata un'eccezione,
d(ptr) viene eseguito.Original:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. If an exception is thrown,
d(ptr) is executed.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8-10)
11)
std::bad_weak_ptr se
r.expired == true. Il costruttore non ha alcun effetto in questo caso.Original:
std::bad_weak_ptr if
r.expired == true. The constructor has no effect in this case.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
12)
std::bad_alloc se richiesto ulteriore memoria non può essere ottenuto. Può lanciare implementazione definita un'eccezione per altri errori. Questo costruttore non ha alcun effetto se si verifica un'eccezione.
Original:
std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. This constructor has no effect if an exception occurs.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
13)
Se viene generata un'eccezione, il costruttore non ha effetti.
Original:
If an exception is thrown, the constructor has no effects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Esempio
#include <memory>
#include <iostream>
struct Foo {
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
};
struct D {
void operator()(Foo* p) const {
std::cout << "Call delete for Foo object...\n";
delete p;
}
};
int main()
{
// constructor with no managed object
std::shared_ptr<Foo> sh1;
// constructor with object
std::shared_ptr<Foo> sh2(new Foo);
std::shared_ptr<Foo> sh3(sh2);
std::cout << sh2.use_count() << '\n';
std::cout << sh3.use_count() << '\n';
//constructor with object and deleter
std::shared_ptr<Foo> sh4(new Foo, D());
}
Output:
Foo...
2
2
Foo...
Call delete for Foo object...
~Foo...
~Foo...
Vedi anche
crea un puntatore condiviso che gestisce un nuovo oggetto Original: creates a shared pointer that manages a new object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |
crea un puntatore condiviso che gestisce un nuovo oggetto allocato con un allocatore Original: creates a shared pointer that manages a new object allocated using an allocator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |