initialiser struct contenir des références à des structs

Est-il possible d'avoir une structure contenant des références à des structures. Et comment sont-ils initialisé? Voir petit exemple ci-dessous.

Grâce

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
}

Ok merci pour tous les commentaires. J'ai également eu à modifier la définition de type de l'typeOuter pour le faire fonctionner. Travail plein de code ci-dessous pour d'autres personnes à trouver ce post.

#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, typedef struct {...} foo; n'est pas "idiomatiques" en C++, il suffit de la rédaction struct foo {...}; est plus habituel (et ne vous permettent d'utiliser foo comme un nom de type directement).

OriginalL'auteur monkeyking | 2013-02-09