On Thursday, July 16, 2015 01:20:10 seashell86 via Digitalmars-d-learn wrote:
> On Thursday, 16 July 2015 at 00:39:29 UTC, H. S. Teoh wrote:
> > On Thu, Jul 16, 2015 at 12:18:30AM +0000, seashell86 via
> > Digitalmars-d-learn wrote:
> >> [...]
> >
> > The reason is that class variables cannot be overridden, only
> > class methods can.
> >
> > If you want to simulate overriding of class variables, you can
> > use a @property method instead:
> >
> >     class Animal {
> >         @property string voice() { return "Wah!"; }
> >         void speak() { writeln(voice); }
> >     }
> >
> >     class Dog : Animal {
> >         override @property string voice() { return "Whoof!"; }
> >     }
> >
> >
> > T
>
> Wow, a quick and thorough response! Thank you!  I'm actually
> curious as to why this feature would not be in the language?  Is
> it for performance reasons?

I've never heard of a language that overloaded member variables, and given
how class polymorphism works, I don't see how it would even be possible
without making it so that all accesses to a variable actually call a
function to access it, which would needlessly inefficient (polymorphism
works by looking up the correct function in the class' virtual table, and
the call site isn't going to know at compile time what class it's actually
operating on, so all of that has to be done at runtime). And if you really
want something approximating overriding variables, then you can just use
property functions to access the variable rather than accessing it directly,
and then you can override the property functions.

- Jonathan M Davis

Reply via email to