Virtual method call from constructor

Part of TutorialAdvanced, ClassesCategory.

Description

Subclass virtual methods can be called from parent constructor.

This could not be done in C++ due to multiple inheritance.

Example

import std.stdio;

class A
{
  this () {
    foo ();
  }

  void foo () {
    writefln ("foo from A");
  }
}

class B : A
{
  this () {
    super ();
  }

  void foo () {
    writefln ("foo from B");
  }
}

void main ()
{
  A a = new A (); // writes "foo from A"
  B b = new B (); // writes "foo from B"
}

Source

Author billitch
Posted by billitch
Date/Time Thu Jun 14 06:11:57 CEST 2007