6

I read about structure in c++ that it can not contain instance of itself. Can anybody help me to understand why it can not contain instance of itself?

Learner
  • 1,544
  • 8
  • 29
  • 55

7 Answers7

9

Because then it would take up "infinite" storage since when it is initialised it recursively initialises itself. You can store pointers to the same structure, however.

e.g. This is invalid:

struct a
{
    int someVar;
    a bad;
};

This is valid (say if you want a linked list of this structure):

struct a
{
    int someVar;
    a* good;
};
Alex Z
  • 2,500
  • 2
  • 19
  • 23
6

Because to create the instance of it, you will need to create the variable, which is itself an instance of it - which will invoke the constructor.

This will result in infinite recursive call to the constructor.

Assume class A has an instance variable named a:
Invoking the constructor of A will cause the initialization of a, which is itself an A. To do it - the constructor of A will be invoked again.

Note that it will not even compile because the compiler cannot allocate the memory for it, it doesn't know how much space to allocate for each object. How much space does it take to store the instance variable a? [Any finite space will not be enough because there will always be an extra variable which also needs to be allocated]

amit
  • 175,853
  • 27
  • 231
  • 333
  • 1
    "How much space does it take to store the instance variable a" - as a special case, any size would work provided that `A` has no other data members or base classes that occupy space. But it's hardly worth allowing it only in a case where it would be useless. – Steve Jessop Feb 20 '12 at 12:03
  • It should be pointed out that you can create an instance dynamically and store a pointer to it in the same struct, basically creating a forward list. `struct S{ int data; S* next; }` – zett42 Jul 04 '17 at 20:17
  • how does java works then ? – Learner Jul 05 '17 at 16:53
  • @Learner Java objects allocation are references. `class A { A a; }` means there is reference to `A`, not the object itself. This means you don't need to allocate an `A` inside it, and your constructor doesn't have to allocate (and call its constructor recursively). – amit Jul 05 '17 at 17:30
5
struct bedroom
{
    bed b;
    table t;
    bedroom r;
};

Do you see the problem now? A bedroom would need storage for an infinite amount of beds and tables.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
3

Because then it would lead to inifite recursion. e.g.

struct foo
{
    int boo;
    foo f;
};

foo f;

foo would contain a foo which contains a foo etc...

To get around it you should use a pointer:

struct foo
{
    int boo;
    foo* f;
};
ronag
  • 49,529
  • 25
  • 126
  • 221
1

not just structure any incomplete data type cannot be used with in it. lets say u have a structure A and u have included the same structure as a member,now when compiler tries to allocate memory to it how does it know how much to allocate because u have declared structure A inside it which is not yet completely defined and it will throw an error incomplete data type not allowed.

ken
  • 816
  • 1
  • 9
  • 25
1

As other answers point out, the structure cannot contain an instance of itself, because that would result in an infinite recursion when creating the structure. However a structure can contain a pointer to itself:

struct foo
{
    int boo;
    foo *f;
};

foo f;
f.f = &f;
Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

Because it is impossible to create memory layout for such structure. If a struct foo contains an int and a foo, how would you fit sizeof(int)+sizeof(foo) bytes into sizeof(foo) bytes? The equation A+B=A does not have any solutions for A,B > 0.

hamstergene
  • 24,039
  • 5
  • 57
  • 72