◐ Shell
clean mode source ↗

Déclaration d'une union — cppreference.com

De cppreference.com

<metanoindex/>

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:

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.

Syntaxe

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

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.

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.

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.