D - more or less power than C++?

Hasan Aljudy hasan.aljudy at gmail.com
Sun Mar 5 21:34:06 PST 2006


Johan Granberg wrote:
> Ben Phillips wrote:
> 
>> In article <dubpge$u3r$1 at digitaldaemon.com>, Johan Granberg says...
>>
>>>>> 1. const parameters and member functions
>>>>>
>>>>> Countless times this saved me. I just can't imagine references being
>>>>> passed in and out of functions without something explicitly saying 
>>>>> that
>>>>> the function is expected or not to modify it.
>>>
>>> You did not answer the above statement and i have seen this repeated 
>>> all over this thread along with destructors in structs.
>>>
>>
>> A method can explicitly say that it will modify a reference by using 
>> "out" and
>> "inout".
>>
>> I've never cared much for const member methods, so I'm confused as to 
>> how a lack
>> of them can cause such a problem. Can you give an example?
>>
>>
> 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?



More information about the Digitalmars-d mailing list