D Language
Structs
Recherche…
Définir une nouvelle structure
Pour définir la structure appelée Personne avec un type entier variable age, type entier variable variable de type float ageXHeight:
struct Person {
int age;
int height;
float ageXHeight;
}
Généralement:
struct structName {
/+ values go here +/
}
Constructeurs Struct
En D, nous pouvons utiliser des constructeurs pour initialiser des structures comme une classe. Pour définir une construction pour la structure déclarée dans l'exemple précédent, nous pouvons taper:
struct Person {
this(int age, int height) {
this.age = age;
this.height = height;
this.ageXHeight = cast(float)age * height;
}
}
auto person = Person(18, 180);
Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow