Construct immutable member in derived class
Alex
sascha.orlov at gmail.com
Thu Apr 5 13:36:07 UTC 2018
On Wednesday, 4 April 2018 at 21:49:08 UTC, Timoses wrote:
> "[...] the construction of the base class can be independent
> from the derived one."
>
> Hm, the points 7 and 8 don't clearly state what you wrote.
Yes :)
> But it somehow does make sense.. Still I wonder why that is so.
>
> Let's say you have an abstract class with immutable members.
> Why shouldn't derived class constructors be allowed to
> initialize these immutable members?
My reason is a semantic one, so it's rather how I'm explaining
this to me, then a formal one.
Let's assume, we abstract away a member from different derived
classes to an abstract class, which cannot be handled by this
abstract class.
Ok, this can happen, so it would be ok, if the base class does
not handle the var.
But then, we add a qualifier. How can this be? As the abstract
class cannot handle the abstraction, it also can not add any
qualifiers.
As the qualifier is there, then, the base class at least has
knowledge about the immutability. And due this fact, its own
constructor has to handle this variable in some way.
However, this a minor problem, isn't it? If you argue, that you
can also abstract the immutability qualifier, then, I would say,
that a derived class always has a better knowledge how to handle
its members, and therefore how to call the base class
constructor, as it knows which class it is derived from.
So, for short:
Either: the immutability belongs to the base class and therefore
it has to manage the var
Or: it doesn't and the derived classes have the knowledge how to
serve their base class.
More information about the Digitalmars-d-learn
mailing list