Namespace
Varianti

std::shared_ptr::shared_ptr

Da cppreference.com.
< cpp‎ | memory‎ | shared ptr

 
 
Utilità libreria
Tipo di supporto (basic types, RTTI, type traits)
Gestione della memoria dinamica
La gestione degli errori
Programma di utilità
Funzioni variadic
Data e ora
Funzione oggetti
initializer_list(C++11)
bitset
hash(C++11)
Gli operatori relazionali
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Coppie e tuple
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Swap, in avanti e spostare
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
Gestione della memoria dinamica
Basso livello di gestione della memoria
Allocatori
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
allocator
allocator_traits(C++11)
allocator_arg_t(C++11)
allocator_arg(C++11)
uses_allocator(C++11)
scoped_allocator_adaptor(C++11)
Non inizializzata stoccaggio
Original:
Uninitialized storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
uninitialized_copy
uninitialized_copy_n(C++11)
uninitialized_fill
uninitialized_fill_n
raw_storage_iterator
get_temporary_buffer
return_temporary_buffer
Puntatori intelligenti
Original:
Smart pointers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(C++11)
shared_ptr(C++11)
weak_ptr(C++11)
auto_ptr(deprecato)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
Garbage collection supporto
Original:
Garbage collection support
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declare_reachable(C++11)
undeclare_reachable(C++11)
declare_no_pointers(C++11)
undeclare_no_pointers(C++11)
pointer_safety(C++11)
get_pointer_safety(C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pointer_traits(C++11)
addressof(C++11)
align(C++11)
C Library
Original:
C Library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
std::shared_ptr
Membri funzioni
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
shared_ptr::shared_ptr
shared_ptr::~shared_ptr
shared_ptr::operator=
Modificatori
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
shared_ptr::reset
shared_ptr::swap
Osservatori
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
shared_ptr::get
shared_ptr::operator*
shared_ptr::operator->
shared_ptr::use_count
shared_ptr::unique
shared_ptr::operator bool
shared_ptr::owner_before
Non membri funzioni
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::swap
make_shared
allocate_shared
static_pointer_cast
dynamic_pointer_cast
const_pointer_cast
get_deleter
operator==
operator|=
operator<
operator<=
operator>
operator>=
operator<<
atomic_is_lock_free
atomic_load
atomic_load_explicit
atomic_store
atomic_store_explicit
atomic_exchange
atomic_exchange_explicit
atomic_compare_exchange_weak
atomic_compare_exchange_strong
atomic_compare_exchange_weak_explicit
atomic_compare_exchange_strong_explicit
std::hash
 
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.
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.
1)
Costruttore di default costruisce un shared_ptr senza oggetto gestito, shared_ptr cioè vuoto
Original:
Default constructor 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.

Indice

[modifica] 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.

[modifica] 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.

[modifica] Eccezioni

1)
noexcept specification:  
noexcept
  (dal C++11)
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.
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.
5)
noexcept specification:  
noexcept
  (dal C++11)
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.
8-10)
noexcept specification:  
noexcept
  (dal C++11)
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.
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.
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.

[modifica] 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...

[modifica] 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) [edit]
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) [edit]