[erlang-questions] Fear and Loathing in Programming La La Land

Michael Schreckenbauer grimlog@REDACTED
Sat Apr 7 14:30:36 CEST 2012


On Wednesday 04 April 2012 23:24:04 Jan Burse wrote:
>      Notice that in C++ it is not necessary —
>      and in fact impossible — to provide a
>      specialized constructor for const instances.
> 
> I am not an expert for C++, but when the above
> is true, then it is clear why setters are needed
> and everything goes havoc. For example Java
> doesn't have the above restriction, an immutable
> object can have multiple constructors:
> 
>      class myKeyImmutableByConstructor {
>          int alfa;
>          int beta;
> 
>          myKeyImmutableByConstructor(int a, int b) {
>             alfa = a;
>             beta = b;
>          }
> 
>          myKeyImmutableByConstructor() {
>             alfa = 0;
>             beta = 0;
>          }
>      }
> 

you are misreading the statement. Of course you can have multiple-constructors 
for immutable objects in C++ as in java.
The statement talks about const *instances*,
myKeyImmutableByConstructor and const myKeyImmutableByConstructor have the 
same constructors available,so you can write
myKeyImmutableByConstructor inst;
myKeyImmutableByConstructor inst(1, 2);
const myKeyImmutableByConstructor const_inst;
const myKeyImmutableByConstructor const_inst(1, 2);
You cannot restrict constructors to be usable only with const_inst.

> Bye

Best,
Michael



More information about the erlang-questions mailing list