[Bug 191] New: Cannot refer to member variable in default value for method parameter

Deewiant deewiant.doesnotlike.spam at gmail.com
Sun Jun 11 12:37:53 PDT 2006


Jarrett Billingsley wrote:
> "Deewiant" <deewiant.doesnotlike.spam at gmail.com> wrote in message 
> news:e6hp54$a2d$1 at digitaldaemon.com...
>> Especially since the workaround is so simple, and isn't that all that
>> default parameters are meant to do - overload the method into a form which
>> takes one less parameter and passes the default value to the original?
> 
> Well, as far as I know, that's not how the compiler implements it.  It just 
> keeps one copy of the function, and when it comes across a call that doesn't
>  have all the parameters, it rewrites the function call so that the unwritten
>  parameters use the parameter initializer.  So there's no actual overloading
>  going on.
> 

If it does it like that - which _would_ be simpler and possibly faster for the
non-class-member case - I can sort of see why it might be tricky for the
class-member case.

But it's still not impossible, like you pointed out: calling a method, the
compiler knows which class instance's method is being called and can take the
member from the same instance.

It does, however, become impossible if the member is private or protected, and
the caller can't access the variable. Which, come to think of it, is probably
why it's not allowed in C++.

Simulating that extra overloaded method does work even in that case, though.



More information about the Digitalmars-d-bugs mailing list