A new class -->String

jinheking caoqi at bgi.net.cn
Wed Apr 4 03:18:06 PDT 2007


Your advice is right
I changed my fun below:
/**
  *
  * <code>String str=new String("abc");</code>
  * <code>str="abcd";</code>
  * @param  wc
    *         The initial value of the string
  */
  String opAssign(wchar[] wc) {
   this.offset = 0;
   this.count = wc.length;
   this.value = wc.dup;
   return this;
    }

void test(){
    String str=new String("abc"); //True
    str="abcd"; //True
    String s2="aa";//false  //which reason
}

How do do it?



"Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> дÈëÏûÏ¢ÐÂÎÅ:euvsh7$ekg$1 at digitalmars.com...
> jinheking wrote:
>> String opAssign(wchar[] value) {
>>    int size = value.length;
>>    offset = 0;
>>    count = size;
>>    value = value;
>>    return this;
>>     }
>>
>> That is this funcation!
>
> I'm sorry, I don't understand what you're trying to say.
>
> If you were confused about my comment on that function in particular, let 
> me try to reword it:
> The way I would code that function (assuming the class fields stay the 
> same) would be like so:
> ---
> String opAssign(wchar[] value) {
>     offset = 0;
>     count = value.length;
>     this.value = value.dup;
>     return this;
> }
> ---
> The 'this.' makes sure it assigns to the 'value' defined at the top of the 
> class, not the one parameter.
> The '.dup' makes a copy of the string passed in to make sure nobody else 
> can change the value of the String object, since you want it to be 
> immutable. 





More information about the Digitalmars-d mailing list