Nested Classes with inheritance

Salih Dincer salihdb at hotmail.com
Sat Mar 19 00:05:54 UTC 2022


Greetings to all...

There are nested classes as below. But beware, there's also 
inheritance, extra! If you construct ```Bar b``` from main(), 
it's okay. But if declare the constructor in Foo(), the program 
crashes with a segmentation error.

Is this not legal? Like two mirrors are facing each other, that I 
do?

```d
class Foo
{
     Bar b;
     int i;

     this(int n)
     {
         this.i = n;
         //this.b = new Bar(this.i); // compiles but crashes
     }

     class Bar : Foo
     {
         int i;

         this(int n)
         {
             this.i = n;
             super(this.i);
         }

     }
}

void main()
{
     auto foo = new Foo(1);
     auto bar = foo.new Bar(1);
     foo.b = bar;

     assert(foo.i == foo.b.i);
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list