Est-il possible d'avoir une structure contenant des références à des structures. Et comment sont-ils initialisés? Voir le court exemple ci-dessous.initialize struct contient des références aux structures
Merci
typedef struct {
int a;
}typeInner1;
typedef struct {
int b;
}typeInner2;
typedef struct {
typeInner1 &one;
typeInner2 &two;
}typeOuter;
void fun2(typeOuter *p){
p->one.a =2;
p->two.b =3;
}
void fun(typeInner1 &arg1,typeInner2 &arg2){
typeOuter *ptr = new typeOuter;//<-- how to write initializer
fun2(ptr);
}
int main(){
typeInner1 arg1;
typeInner2 arg2;
fun(arg1,arg2);
//now arg1.a should be 2 and arg2.a=3
}
de Ok merci pour toutes les entrées. J'ai aussi dû modifier le typedef du typeOuter pour le faire fonctionner. Plein code de travail ci-dessous pour d'autres personnes trouvant ce poste.
#include <cstdio>
typedef struct {
int a;
}typeInner1;
typedef struct {
int b;
}typeInner2;
typedef struct typeOuter_t {
typeInner1 &one;
typeInner2 &two;
typeOuter_t(typeInner1 &a1, typeInner2 &a2) : one(a1), two(a2) {}
}typeOuter;
void fun2(typeOuter *p){
p->one.a =2;
p->two.b =3;
}
void fun(typeInner1 &arg1,typeInner2 &arg2){
typeOuter *ptr = new typeOuter(arg1,arg2);
fun2(ptr);
}
int main(){
typeInner1 arg1;
typeInner2 arg2;
fun(arg1,arg2);
//now arg1.a shoule be 1 and arg2.a=3
fprintf(stderr,"arg1=%d arg2=%d\n",arg1.a,arg2.b);
}
BTW, struct 'typedef {...} foo,' est pas "idiomatiques" en C++, écrire simplement 'struct foo {...};' est plus courant (et permet d'utiliser directement 'foo' comme nom de type). – Mat