Overriden method not detected ?
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 4 09:55:36 PDT 2016
On 06/04/2016 05:02 PM, chmike wrote:
> Is it possible to instantiate immutable objects by using emplace
Yes. I'm not sure, but the memory may have to be untyped for the emplace
call to avoid mutating immutable data. I.e., call emplace with void[],
not with a pointer whose target is already type as immutable.
> and
> modify the object in the toString() call ?
No. immutable means the object cannot, will not, must not ever change
after construction.
> This is to cache the
> resulting string to avoid creating a new string at each call.
>
> I would have to cast away the immutable attribute of 'this'. Is this
> possible ?
You can cast immutable away, but as soon as you mutate the object, you
have an invalid program. You'd rely on undefined behavior.
> Note that the objects would be instantiated by emplace in a void array
> at start up. So the memory is writable.
Doesn't matter. The compiler is free to assume that the data doesn't
change after construction.
The compiler will let you cast away immutable and mutate then. It may
work out as you expect. It's still undefined behavior, and I advise
against doing it.
More information about the Digitalmars-d-learn
mailing list