Construct immutable member in derived class

Alex sascha.orlov at gmail.com
Thu Apr 5 21:09:08 UTC 2018


On Thursday, 5 April 2018 at 19:31:39 UTC, Jonathan M Davis wrote:
> And you can't abstract whether a member variable is marked with 
> immutable or not. That's part of the variable. Declaring an 
> immutable instance of an object would then treat the member 
> variable in immutable for that instance, so it's possible to 
> have an immutable member variable when the member variable is 
> not itself marked with immutable, but class inheritance has 
> nothing to do with controlling which type qualifiers were used 
> on the member variable of a base class. Derived classes 
> override member functions. They don't override member variables 
> or anything about them. They override the behavior of the base 
> class, not the structure.

That beats my argumentation. Fully agree with that.

> For them to do otherwise would become quite problematic if you 
> ever use a derived class through a base class reference. They 
> can add onto the structure of a base class, but the derived 
> class must be usuable via a base class reference, and trying to 
> do something like have the derived class alter the qualifiers 
> on base class member variable simply would not work with that. 
> That sort of thing could only ever work if the base class were 
> just a way to add functionality to a derived class rather than 
> having anything to do with references, and that's simply not 
> how classes work in D. If that's the sort of thing that you 
> want, it would make more sense to add the functionality via 
> composition rather than inheritance.

Yeah... little bit of mixing things up in my head, maybe... Did 
exactly this stuff during the last couple of months.

Thanks a lot!


More information about the Digitalmars-d-learn mailing list