Espaces de noms
Variantes
Affichages
Actions

Déclaration d'une union

De cppreference.com
< cpp‎ | language

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
unions de types
types fonctions
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 
Une union est un type de classe spéciale qui stocke l'ensemble de ses membres de données dans le même emplacement mémoire.
Original:
A union is a special class type that stores all of its data members in the same memory location.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Les unions ne peuvent pas avoir de fonctions virtuelles, ne peuvent être héritées ou hériter d'autres classes.
Original:
Unions cannot have virtual functions, be inherited or inherit other classes.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Les unions (avant C++11) ne peuvent contenir que les types BVD (bonnes vieilles données) .
Original:
(avant C++11) Unions can only contain POD (plaine anciennes données) types.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(depuis C++11) Si une union contient un élément non-BVD, qui a une fonction particulière définie par l'utilisateur (constructeur, destructeur, constructeur par copie ou affectation par copie) cette fonction est supprimée par défaut dans l' union et doit être explicitement définie par l'utilisateur .
Original:
(depuis C++11) If a union contains a non-POD member, which has a user-defined special function (constructor, destructor, copy constructor or copy assignment) that function is deleted by default in the union and needs to be defined explicitly by the user.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Sommaire

[modifier] Syntaxe

union name { member_declarations } object_list (en option) ; (1)
union { member_declarations } ; (2)

[modifier] Explication

# Union nomée
Original:
#Named union
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Union anonyme
Original:
#Anonymous union
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier] Unions anonymes

Les membres d'une union anonyme sont accessibles à partir de la portée englobante comme des simples variables.
Original:
Members of an anonymous union are accessible from the enclosing scope as single variables.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Les unions anonymes ont d'autres restrictions: elles doivent seulement comporter des membres publiques et ne peuvent pas avoir des fonctions membres.
Original:
Anonymous unions have further restrictions: they must have only public members and cannot have member functions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Les unions dans la portée d'un espace de noms doivent être statiques.
Original:
Namespace-scope anonymous unions must be static.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifier] Exemple

union foo
{
  int x;
  signed char y;
};
 
int main()
{
  foo.x = 128 + 896;
  std::cout << "comme int: "  << (int)foo.x << '\n';
  std::cout << "comme char: " << (int)foo.y << '\n';
  return 0;
}

Résultat :

comme int: 1024
comme char: 128
(Pour processeurs petit-boutistes)
Original:
(for little-endian processors)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.