inheriting constructos

Michael Rynn michaelrynn at optusnet.com.au
Sun Mar 7 23:04:50 PST 2010


On Fri, 04 Dec 2009 10:05:16 +0200, Lionello Lunesu wrote:

> Perhaps reusing a constructor of an inherited class should be easier.
> Something like "alias this"? But how to refer to specific ctor
> overloads?
> 
> L.
> 
> On 30-11-2009 1:03, Andrei Alexandrescu wrote:
>> Walter and I just discussed the matter of inheriting constructors. Our
>> thought on the issue:
>>
>> a) If a class doesn't define any constructors and adds no fields,
>> inherit constructors. Example:
>>
>> class MyException : Exception {}
>>
>> b) If a class defines at least one constructor, do not inherit
>> constructors.
>>
>> c) If a class doesn't define any constructors but does add at least a
>> non-static field -> undecided.
>>
>> What do you think?
>>
>>
>> Andrei


I do not want the compiler to decide this for me.

I do have cases where the constructor  has to be repeated for each 
generation of child classes for the values that will eventually set for 
some parent class.  You can object there must be something wrong with the 
design here.

But would it not be possible to indicate in the child class that the 
constructor is the same as the parent.
	class parent {
		this(t1 everybody, t2 gets, t3 it){}
	}
	class child : parent {
		this(t1 everybody, t2 gets, t3 it)
		{
			super(everybody, gets, it);
		}
	}

Would like if the compiler could optimise the call to child.this away.

	or just shorten it indicate the constructor is the same (without 
using another key word )
	
	Such a shortening, using a '.' or ':' would be transitive.
	class child : parent {
		//calls parent.this
		this.super(t1 everybody, t2 gets, t3 it);

	}
	class gchild : child {
		//calls parent.this
		this.super(t1 everybody, t2 gets, t3 it); 
	}
	




More information about the Digitalmars-d mailing list