lang:cpp:smartptr
                Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| lang:cpp:smartptr [2019/10/17 08:45] – Création avec "std::weak_ptr en tant que clé dans un conteneur" root | lang:cpp:smartptr [2025/07/06 16:33] (Version actuelle) – [Préserver l'attribut const sur les pointeurs] : déplacement dans un article dédié root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | {{ : | ||
| + | |||
| + | ====Durée de vie==== | ||
| + | |||
| + | Lors d'un '' | ||
| + | |||
| + | <code cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | class A { | ||
| + |  | ||
| + | A() { std::cout << " | ||
| + | ~A() { std::cout << " | ||
| + | }; | ||
| + | |||
| + | int main() { | ||
| + | std:: | ||
| + | a.reset(new A()); | ||
| + | a = std:: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Sortie : | ||
| + | |||
| + | < | ||
| + | Constructor | ||
| + | Constructor | ||
| + | Destructor | ||
| + | Constructor | ||
| + | Destructor | ||
| + | Destructor | ||
| + | </ | ||
| + | |||
| + | |||
| + | ====Encapsuler fopen/ | ||
| + | |||
| + | <code cpp> | ||
| + | std:: | ||
| + | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | ====weak_ptr==== | ||
| + | |||
| ====std:: | ====std:: | ||
| Il faut utiliser le comparateur '' | Il faut utiliser le comparateur '' | ||
| Ligne 5: | Ligne 50: | ||
| </ | </ | ||
| + | ===Différencier empty et expired=== | ||
| + | Sans passer par '' | ||
| + | |||
| + | <code cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | class A {}; | ||
| + | |||
| + | int main() { | ||
| + | { | ||
| + | std:: | ||
| + | |||
| + | if (!aa.owner_before(std:: | ||
| + | std::cout << " | ||
| + | } | ||
| + | if (aa.expired()) { | ||
| + | std::cout << " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | { | ||
| + | std:: | ||
| + | std:: | ||
| + | |||
| + | if (!aa.owner_before(std:: | ||
| + | std::cout << " | ||
| + | } | ||
| + | if (aa.expired()) { | ||
| + | std::cout << " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | { | ||
| + | std:: | ||
| + | { | ||
| + | std:: | ||
| + | aa = a; | ||
| + | |||
| + | if (!aa.owner_before(std:: | ||
| + | std::cout << " | ||
| + | } | ||
| + | if (aa.expired()) { | ||
| + | std::cout << " | ||
| + | } | ||
| + | } | ||
| + | if (!aa.owner_before(std:: | ||
| + | std::cout << " | ||
| + | } | ||
| + | if (aa.expired()) { | ||
| + | std::cout << " | ||
| + | } | ||
| + | } | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
lang/cpp/smartptr.1571294723.txt.gz · Dernière modification :  de root
                
                