D - more or less power than C++?
Johan Granberg
lijat.meREM at OVEgmail.com
Mon Mar 6 08:21:56 PST 2006
Hasan Aljudy wrote:
>> It is not const methods so much as const in parameters.
>>
>> class A
>> {
>> int bar=1;//bar should bee read only
>> }
>>
>> void foo(in i a)//I want a to bee readonly
>> {
>> i.bar=2;//here the value should not be writable but since a is
>> //a reference it is
>> }
>>
>> A a=new A;//bar is 1
>> foo(a);
>> print(a.bar);//prints 2
>>
>> It is not so very usefull in smal examples but if you have a large
>> program and do that by mistake it could cause a bug. Basicaly it is a
>> message from the writer of a class to it's users of how member are to
>> bee used, and aditionaly it has compiler suport to catch typos and
>> mistakes, if a user realy want to change the data against the orginal
>> authors recomendations he can use a explicit cast.
>
> Maybe I'm missing something, but why don't you just mak A.bar private?
Because then i would have to call a method to read bar and that would
bee inefective. I know it is no big deal when bar is only an ont but
when larger structures is copied the overhead can bee a significant
slowdown. also privat does not protect from misstakes in the same class
but if you do a cast you will think about why it is readonly in the
first place.
More information about the Digitalmars-d
mailing list