What's the authoritative difference between immutable and const for reference types?

Justin Johansson no at spam.com
Sat Jun 26 04:42:24 PDT 2010


Simen kjaeraas wrote:
> Justin Johansson <no at spam.com> wrote:
> 
>> Specifically, I wish to have class which has a member variable which 
>> cannot be changed (and is guaranteed not to change) and this member 
>> variable happens to be a reference type (i.e. it's a pointer in C++ 
>> parlance) and, further more, the instance of the class which that 
>> variable refers to is to be deep immutable.
>>
>> For instance, with
>>
>> class Foo
>> {
>> }
>>
>> class Bar
>> {
>>     Foo foo;
>> }
>>
>> consider instances of Foo to be in ROM and instances of Bar to be in 
>> RAM   and once a Bar instance is constructed, the member variable foo 
>> itself is not allowed to be modified.
> 
> What you want is
> 
> class Bar
> {
>     immutable Foo foo;
> }
> 
> Now, I believe there are some problems constructing immutable objects, for
> which the assumeUnique template in std.contracts is created.
> 

Thanks for that Simen.

Thinking about this a bit more, there are four possibilities as 
indicated in the following table :-

   Variable foo is modifiable | Data referred to by foo is modifiable
   ---------------------------+--------------------------------------
          No                  |          No
          No                  |          Yes
          Yes                 |          No
          Yes                 |          Yes
   ---------------------------+--------------------------------------

What combination of immutable and const storage classes make for the 
implementation of these four possibilities?









More information about the Digitalmars-d mailing list