rename this to ctor
davidl
davidl at 126.com
Wed Apr 1 04:00:27 PDT 2009
在 Wed, 01 Apr 2009 18:36:56 +0800,Christian Kamm
<kamm-incasoftware at shiftatleftanddelete.de> 写道:
> Walter Bright Wrote:
>> Instead, you can do this:
>>
>> class inherit : base
>> {
>> ctor(int i) { }
>> ctor() { super(); }
>> }
>
> Yes, but it gets uncomfortable when you want to forward more
> constructors:
>
> class base {
> ctor(int never, MyTemplate!(float, 3, "abcd") ending, char[] parameter
> = "abcd", int list = 3) {}
> ctor(float including, int defaults = 3) {}
> }
>
> class derived : base {
> ctor(int never, MyTemplate!(float, 3, "abcd") ending, char[] parameter
> = "abcd", int list = 3)
> { super(never, ending, parameter, list); }
> ctor(float including, int defaults = 3)
> { super(including, defaults); }
> }
>
> plus you have to replicate default values and make sure you update all
> derived classes when changing the base constructors. I.e. the same
> arguments that explain why you can use alias to bring base class member
> functions into the overload set apply to the constructor. Also,
> sometimes the base class constructors are unknown:
>
> class C(T) : T { this(?) { super(?); }
>
> I guess these issues could also be solved using a mixin if there was a
> way to enumerate constructors at compile time, but there's another
> advantage to renaming 'this' to a non-keyword name: you allow getting
> its address and using it as a template alias parameter.
>
Yeah, all the merits of ctor just outweigh its demerits. The only demerit
of it is taking "ctor" to join the keyword list, However it also bans
people from using ctor as a var( it might be positive! )
More information about the Digitalmars-d
mailing list