lang:cpp:variable
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
lang:cpp:variable [2021/11/05 23:23] – Ajout de "non-const lvalue reference to type" root | lang:cpp:variable [2025/07/28 10:36] (Version actuelle) – Précision sur l'utilisation de la lambda dans "Initialisation complexe d'une constante" root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====Déclaration des variables==== | ====Déclaration des variables==== | ||
+ | ===Pointeur sur méthode vs std:: | ||
+ | |||
+ | '' | ||
+ | |||
+ | De même, il faut préfixer par un '' | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | |||
+ | int main() { | ||
+ | auto funcPtr = +[] {}; | ||
+ | static_assert(std:: | ||
+ | void (*funcPtr2)() = [] {}; | ||
+ | static_assert(std:: | ||
+ | // funcPtr3 est un std:: | ||
+ | auto funcPtr3 = [] {}; | ||
+ | // error: static assertion failed | ||
+ | static_assert(std:: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
===Pointeur sur la méthode d'une classe=== | ===Pointeur sur la méthode d'une classe=== | ||
Ligne 18: | Ligne 41: | ||
[[https:// | [[https:// | ||
- | ====Programmation fonctionnelle==== | ||
- | ===Initialisation complexe d'une constante=== | + | ===Pointeur de pointeur=== |
- | Plutôt que | + | <code cpp> |
+ | struct Z { | ||
+ | int z; | ||
+ | }; | ||
- | {{gh> | + | struct A { |
+ | Z* i; | ||
+ | }; | ||
- | écrire | + | struct B { |
+ | A a; | ||
+ | }; | ||
- | {{gh> | + | int main() |
+ | auto ptr1 = & | ||
+ | auto ptr2 = &A::i; | ||
- | ====Erreurs==== | + | B b; |
- | * '' | + | |
+ | int z = (b.*ptr1.*ptr2)->z; | ||
+ | } | ||
+ | </code> | ||
- | Parfois, il n' | + | ===Variable optionnelle=== |
+ | |||
+ | Création une variable '' | ||
<code cpp> | <code cpp> | ||
- | int i; | + | std:: |
- | int& j = i; | + | |
</ | </ | ||
- | Il faut alors soit passer par une référence constante : | + | ====Programmation fonctionnelle==== |
- | <code cpp> | + | ===Initialisation complexe d'une constante=== |
- | int i; | + | |
- | const int& j = i; | + | |
- | </ | + | |
- | ou passer par un pointeur : | + | Plutôt que |
- | < | + | {{gh> |
- | int i; | + | |
- | int* j = &i; | + | écrire |
- | </code> | + | |
+ | {{gh>https:// | ||
+ | |||
+ | <WRAP center round info 60%> | ||
+ | Bien noter que la lambda est exécutée tout de suite avec un '' | ||
+ | </WRAP> | ||
- | [[https:// |
lang/cpp/variable.1636151002.txt.gz · Dernière modification : de root