Immutable objects and constructor ?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 20 19:41:22 PDT 2016


On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote:

> This is confusing and frustrating. In C++ we can write
> MyInfos {
>    . . .
>    // one is a constant pointer to a constant object of type Obj
>    Obj const * const one;
>    . . .
> }
>
> And in main()
>
> Info const * x1 = MyInfos.one;
>
> x1 i a modifiable pointer to a constant object of type Info.
>
> Is this possible in D ? I couldn't find how to do that.

I should have addressed this above. This is another of the those 
things that will bite you if you are thinking in C++ when writing 
D. Classes in D are references types, so right out of the gate 
they can not be treated as C++ classes. You absolutely can have 
modifiable pointers to const and immutable data with built in 
types and structs, but not with classes. Always think of 
const(classref) as const(class*). There is no such thing as 
const(class)* in D.


More information about the Digitalmars-d-learn mailing list