Espaces de noms
Variantes
Affichages
Actions

std::shared_ptr::shared_ptr

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

 
 
 
La gestion dynamique de la mémoire
Faible niveau de gestion de la mémoire
Répartiteurs
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 initialisée stockage
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
Pointeurs intelligents
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 (obsolète)
owner_less (C++11)
enable_shared_from_this (C++11)
bad_weak_ptr (C++11)
default_delete (C++11)
Soutien garbage collection
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)
Divers
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)
Bibliothèque C
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
Les fonctions membres
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=
Modificateurs
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
Des observateurs
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
Tiers fonctions
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)
Construit shared_ptr nouvelle d'une variété de types de pointeurs qui renvoient à un objet à gérer. En option deleter d peut être fourni, qui est ensuite utilisée pour détruire l'objet lorsque aucun objet shared_ptr le posséder. Par défaut, l'expression supprimer l'expression est utilisé comme deleter. delete expression appropriée correspondant au type fourni est toujours sélectionné, c'est la raison pour laquelle les constructeurs sont implémentés comme des modèles à l'aide d'un paramètre distinct Y .
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, supprimer l'expression 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.
Si l'objet pointé par ptr est déjà détenue par certains pointeur partagé, les résultats constructeur à un comportement indéfini .
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)
Constructeur par défaut construit un shared_ptr sans objet géré, c'est à dire shared_ptr vide
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)
Construit un shared_ptr avec ptr que l'objet géré. Y doit être un type complet et implicitement convertible en T. En outre:
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)
Utilise l'expression supprimer le deleter. Une expression valide de suppression doit être disponible, à savoir delete ptr doivent être bien formés, ont un comportement bien défini et ne pas jeter des exceptions .
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)
Utilise le d spécifié deleter le deleter. Deleter doivent être remboursables pour le type T, c'est à dire d(ptr) doivent être bien formés, ont un comportement bien défini et ne pas jeter des exceptions. Deleter doit être 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)
Idem (3), mais utilise en outre une copie de alloc d'attribution de données à usage interne. Alloc doit être un Allocator. Le constructeur de copie et le destructeur ne doit pas lever d'exceptions .
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)
Analogue à (2), (3), (4) respectivement, mais construit un shared_ptr sans objet géré, c'est à dire vide 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)
Construit un shared_ptr qui partage des informations avec la propriété r, mais est titulaire d'un pointeur ptr indépendant et autonome. Même si cette shared_ptr est le dernier du groupe à sortir de la portée, il va appeler le destructeur de l'objet géré à l'origine par r. Toutefois, l'appel get() sur ce renverra toujours une copie de ptr. Il est de la responsabilité du programmeur de s'assurer que cette ptr reste valable aussi longtemps que cette shared_ptr existe, comme dans les cas d'utilisation typiques où ptr est un membre de l'objet géré par r ou un alias (par exemple, abattu) de 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)
Construit un shared_ptr qui partage la propriété de l'objet géré par r. Si r gère aucun objet, aucun objet *this gère aussi. La surcharge templated ne pas participer à la résolution de surcharge si Y* n'est pas implicitement convertible en 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-construit un shared_ptr de r. Après la construction, *this contient une copie de l'état antérieur de r, r est vide. La surcharge templated ne pas participer à la résolution de surcharge si Y* n'est pas implicitement convertible en 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)
Construit un shared_ptr qui partage la propriété de l'objet géré par r. Y* doit être convertible en T*. Notez que r.lock() peuvent être utilisés dans le même but: la différence est que ce constructeur lève une exception si l'argument est vide, tandis que std::weak_ptr<T>::lock() construit une std::shared_ptr vide dans ce cas .
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)
Construit un shared_ptr qui stocke et propriétaire de l'objet ayant appartenu à r. Y* doit être convertible en T*. Après la construction, r est vide .
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)
Construit un shared_ptr qui gère l'objet actuellement géré par r. Le deleter associé à r est stocké pour la suppression future de l'objet géré. r gère aucun objet après l'appel. Y* doit être convertible en 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.

Sommaire

[modifier] Notes

Lors de la construction d'un pointeur brut à un objet d'un type dérivé de std::enable_shared_from_this, les constructeurs de shared_ptr détecter d'autres propriétaires de l'objet à travers la référence faible enregistré dans cette classe de base et l'actionnariat avec eux, au lieu de supposer que l'objet n'est pas géré .
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.

[modifier] Paramètres

ptr -
un pointeur vers un objet à gérer
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 -
un deleter à utiliser pour détruire l'objet
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 allocateur à utiliser pour les allocations de données à usage interne
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 autre pointeur intelligent pour partager la propriété ou d'acquérir la propriété de
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.

[modifier] Exceptions

1)
noexcept specification:  
noexcept
   (depuis C++11)
2)
std::bad_alloc si nécessaire de la mémoire supplémentaire n'a pu être obtenue. Peut générer définie par l'implémentation d'exception pour d'autres erreurs. delete ptr est appelé si une exception se produit .
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 si nécessaire de la mémoire supplémentaire n'a pu être obtenue. Peut générer définie par l'implémentation d'exception pour d'autres erreurs. d(ptr) est appelé si une exception se produit .
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
   (depuis C++11)
6-7)
std::bad_alloc si nécessaire de la mémoire supplémentaire n'a pu être obtenue. Peut générer définie par l'implémentation d'exception pour d'autres erreurs. Si une exception est levée, d(ptr) est exécuté .
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
   (depuis C++11)
11)
std::bad_weak_ptr si r.expired == true. Le constructeur n'a pas d'effet dans cette affaire .
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 si nécessaire de la mémoire supplémentaire n'a pu être obtenue. Peut générer définie par l'implémentation d'exception pour d'autres erreurs. Ce constructeur n'a aucun effet si une exception se produit .
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)
Si une exception est levée, le constructeur n'a pas d'effets .
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.

[modifier] Exemple

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

Résultat :

Foo...
2
2
Foo...
Call delete for Foo object...
~Foo...
~Foo...

[modifier] Voir aussi

crée un pointeur partagé qui gère un nouvel objet
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.

(fonction générique) [edit]
crée un pointeur partagé qui gère un nouvel objet alloué à l'aide d'un allocateur
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.

(fonction générique) [edit]