title: Cannibal Classes
body: <p>For sometime now, I have been trying to wrap my head around the reason why some “cannibal” classes are allowed to compile. </p>
  
  <p>Before I continue, perhaps I should explain what I call a “cannibal” class. Not sure if I just invented that term or if it has been around for a while or even if I am using it correctly but that is not important right now.</p>
  
  <p>I basically call a cannibal class a class that consumes itself. In other words a class whose interface declares members of its own type. For example:</p>
  
  <pre><code>class Foo
  {
      public Foo SomeFoo; 
  }
  </code></pre>
  
  <p>As you can see above, class Foo has a member of type Foo (itself).</p>
  
  <p>Now, the first time I saw this (looong time ago) I didn’t thing it was going to compile but it to my surprise it does compile. The reason why I didn’t thing this would compile is because to me this screams some type of recursive nightmare.</p>
  
  <p>To complicate things a little further, I decided to try the same thing but making the class a struct such as:</p>
  
  <pre><code>struct Foo
  {
      public Foo SomeFoo; 
  }
  </code></pre>
  
  <p>Unfortunately, this does not compile, instead you get error: <strong><em>Struct member 'Foo.SomeFoo' of type 'Foo' causes a cycle in the struct layout</em></strong></p>
  
  <p>To me, a compile error makes more sense that no error but I am sure there most be a logical explanation to this behavior so I was wondering if anyone of you guys could explain this behavior.</p>
  
  <p>Thanks.</p>