www.digitalmars.com         C & C++   DMDScript  

D - references to this before super.

reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
I have tried the following

alias void delegate() testDelegate;

class Test
{
protected:
 testDelegate toCall;
public:
 void testit() { }
 this( testDelegate td ) { toCall = td; }
 this() { this( &testit ); }
}

int main( char[][] args )
{
 Test t = new Test();
 return 0;
}

it will not compile due to the warning
testdel.d(11): reference to this before super()

if I change line 11 from
 this() { this( &testit ); }
to
 this() { super(); this( &testit ); }
I get the error
testdel.d(11): no super class constructor for Object

if I try

class Base { public this() {} }

class Test : Base
{
 ...... as before ....
 this() { super(); this( &testit ); }
}

I get back to the original error
testdel.d(13): reference to this before super()

int this case it is easy to change the constructor to
 this() { toCall = &testit; }

but had these been more processing the solution is to do
class Test
{
protected:
 testDelegate toCall;
public:
 void testit() { }
private:
 void init( testDelegate td ) { toCall = td; }
public:
 this( testDelegate td ) { init( td ); }
 this() { init( &testit ); }
}

as D starts the constuctor with a fully realised object (like Java, unlike
C++)
it should be valid to pass a delegate, or other member that relate to the
object within the constructor
I believe even to the super class "constructor"
so

class OtherTest : Test
{
public:
 void testitMore() { printf( "foo" ); }
private:
public:
 this() { super( &testitMore ); }
}
should also be valid. the reasoning is the same as above AND
had testitMore() been an overridden virtual function it would be valid for
the 'super'
constructor to call it and run 'this' version before 'this' constructor.

Mike.
Nov 02 2002
parent "Walter" <walter digitalmars.com> writes:
Yeah, I'll probably disable that error diagnostic. Seems more trouble than
it's worth. -Walter

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:aq1pjd$30al$1 digitaldaemon.com...
 I have tried the following

 alias void delegate() testDelegate;

 class Test
 {
 protected:
  testDelegate toCall;
 public:
  void testit() { }
  this( testDelegate td ) { toCall = td; }
  this() { this( &testit ); }
 }

 int main( char[][] args )
 {
  Test t = new Test();
  return 0;
 }

 it will not compile due to the warning
 testdel.d(11): reference to this before super()

 if I change line 11 from
  this() { this( &testit ); }
 to
  this() { super(); this( &testit ); }
 I get the error
 testdel.d(11): no super class constructor for Object

 if I try

 class Base { public this() {} }

 class Test : Base
 {
  ...... as before ....
  this() { super(); this( &testit ); }
 }

 I get back to the original error
 testdel.d(13): reference to this before super()

 int this case it is easy to change the constructor to
  this() { toCall = &testit; }

 but had these been more processing the solution is to do
 class Test
 {
 protected:
  testDelegate toCall;
 public:
  void testit() { }
 private:
  void init( testDelegate td ) { toCall = td; }
 public:
  this( testDelegate td ) { init( td ); }
  this() { init( &testit ); }
 }

 as D starts the constuctor with a fully realised object (like Java, unlike
 C++)
 it should be valid to pass a delegate, or other member that relate to the
 object within the constructor
 I believe even to the super class "constructor"
 so

 class OtherTest : Test
 {
 public:
  void testitMore() { printf( "foo" ); }
 private:
 public:
  this() { super( &testitMore ); }
 }
 should also be valid. the reasoning is the same as above AND
 had testitMore() been an overridden virtual function it would be valid for
 the 'super'
 constructor to call it and run 'this' version before 'this' constructor.

 Mike.
Nov 02 2002