1

I want to know if its possible to make this in C++:

struct Hello
{ 
    Hello A, B;
};

I would appreciate any kind of help

  • 1
    No, you can't do this. This makes no logical sense. – Sam Varshavchik Mar 18 '16 at 02:40
  • Look at your code and what it would do during compile time! Hello generates an instance of Hello A & B, then both A & B would also generate instances of Hello A & B and so on. You would run out of memory just from creating a single instance of this one object. Now on the other hand you can store a pointer of its type as does linked lists do which is valid thing to do. – Francis Cugler Mar 18 '16 at 03:42
  • So there's an A, and B, and A.A, and A.B, and B.A, and B.B, and A.A.A, and ... - you see where this is going? – MSalters Mar 18 '16 at 09:54

1 Answers1

5

No because such usage will consume an infinite amount of memory.

You can store pointers to the struct instead, and this is a common way, for example, in implementing a linked list.

struct Hello
{ 
    Hello *A, *B;
};
MikeCAT
  • 73,922
  • 11
  • 45
  • 70