D - more or less power than C++?
Hasan Aljudy
hasan.aljudy at gmail.com
Mon Mar 6 09:50:18 PST 2006
Johan Granberg wrote:
> 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.
You know, CPUs are fast now a days, a single function call is not
"ineffective".
>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.
Objects don't get copied when they are passed to functions ..
> 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.
I don't understand what you mean.
More information about the Digitalmars-d
mailing list