View mode: basic / threaded / horizontal-split · Log in · Help
March 21, 2010
Append char to char-array in function
Hello everyone,

how can I add a char to a char-array by using a other class?

For example:

class A {

  private char[] text;

  this() {
  ...
     text ~= "a";
     new B(text);
     writefln(text);
  ...
  }

}

class B {

  this(char[] text) {
  ...
     text ~= "b";
  ...
  }

}

This should write "ab" in the command line, but only "a" is shown.

... Thanks for help :).
March 21, 2010
Re: Append char to char-array in function
Nrgyzer Wrote:

> Hello everyone,
> 
> how can I add a char to a char-array by using a other class?
> 
> For example:
> 
> class A {
> 
>    private char[] text;
> 
>    this() {
>    ...
>       text ~= "a";
>       new B(text);
>       writefln(text);
>    ...
>    }
> 
> }
> 
> class B {
> 
>    this(char[] text) {
>    ...
>       text ~= "b";
>    ...
>    }
> 
> }
> 
> This should write "ab" in the command line, but only "a" is shown.
> 
> ... Thanks for help :).


No, it shouldn't. What goes in B, stays in B. But if you add magic 'ref' or 'inout' keyword it might work:

class B {

   this(inout char[] text) {
   ...
      text ~= "b";
   ...
   }

}
March 21, 2010
Re: Append char to char-array in function
Bane:
> No, it shouldn't. What goes in B, stays in B. But if you add magic 'ref' or 'inout' keyword it might work:

Use 'ref' only. If the OP also wants to know why that's the solution, the d.learn newsgroup is the right place to ask.

Bye,
bearophile
March 21, 2010
Re: Append char to char-array in function
bearophile Wrote:

> Bane:
> > No, it shouldn't. What goes in B, stays in B. But if you add magic 'ref' or 'inout' keyword it might work:
> 
> Use 'ref' only. If the OP also wants to know why that's the solution, the d.learn newsgroup is the right place to ask.
> 
> Bye,
> bearophile

Darn. I like inout more. Why do good keywords die first?
March 21, 2010
Re: Append char to char-array in function
On 21/03/10 11:40, Bane wrote:
> bearophile Wrote:
>
>> Bane:
>>> No, it shouldn't. What goes in B, stays in B. But if you add magic 'ref' or 'inout' keyword it might work:
>>
>> Use 'ref' only. If the OP also wants to know why that's the solution, the d.learn newsgroup is the right place to ask.
>>
>> Bye,
>> bearophile
>
> Darn. I like inout more. Why do good keywords die first?

inout still works in D1, in D2 it has a new meaning though to save 
writing out a function 3 times for const/immutable/mutable 
(http://www.digitalmars.com/d/2.0/function.html#inout-functions).
March 21, 2010
Re: Append char to char-array in function
Bane Wrote:

> Nrgyzer Wrote:
> 
> > Hello everyone,
> > 
> > how can I add a char to a char-array by using a other class?
> > 
> > For example:
> > 
> > class A {
> > 
> >    private char[] text;
> > 
> >    this() {
> >    ...
> >       text ~= "a";
> >       new B(text);
> >       writefln(text);
> >    ...
> >    }
> > 
> > }
> > 
> > class B {
> > 
> >    this(char[] text) {
> >    ...
> >       text ~= "b";
> >    ...
> >    }
> > 
> > }
> > 
> > This should write "ab" in the command line, but only "a" is shown.
> > 
> > ... Thanks for help :).
> 
> 
> No, it shouldn't. What goes in B, stays in B. But if you add magic 'ref' or 'inout' keyword it might work:
> 
>  class B {
> 
>     this(inout char[] text) {
>     ...
>        text ~= "b";
>     ...
>     }
>  
>  }
> 

Thanks, that's exactly what I needed :)
Top | Discussion index | About this forum | D home