Thread overview
operator overloading: assign(A a);
Oct 28, 2003
Andrew Edwards
Oct 28, 2003
jhenzie
Oct 28, 2003
Juan C
Oct 28, 2003
Philippe Mori
Oct 29, 2003
Walter
October 28, 2003
Is overloading of the assignment opperator not allowed, overlooked, or simply undocumented? Currently, assigning A to B results in B becoming a pointer to A.

ie

class assign
{
  this() {}
  int i;
}

int main()
{
  assign A = new assign;
  assign B = new assign;
  A.i = 10;
  B = A;

  // make changes to A
  // should not affect B
  A.i = 0;
  printf("%d"\t,B.i); // should print 10

  // make changes to B
  // should not affect A
  B.i = -10;
  printf("%d",A.i);  // should print 0;

  return 0;
}

output:

0    -10

I would like to copy the contents of A to B. Is there a way to implement a copy constructor and use the assignment operator to invoke it? Please demonstrate.

Andrew


October 28, 2003
I could be wrong but I thought that A and B are references thus B = A makes B a
reference to the
same instance as is referenced from A, thus changes to A are reflected in B and
visa versa.

The assign created as B is no longer strongly routed and is now a candidate for garbage collection.

The assigment operator does not appear to be overloadable probably because of
reference
semantics thus the necessity for dup or clone funtions within types that support
such behavior.

Hope that helps.

Justin


In article <bnlh1i$2c94$1@digitaldaemon.com>, Andrew Edwards says...
>
>Is overloading of the assignment opperator not allowed, overlooked, or simply undocumented? Currently, assigning A to B results in B becoming a pointer to A.
>
>ie
>
>class assign
>{
>  this() {}
>  int i;
>}
>
>int main()
>{
>  assign A = new assign;
>  assign B = new assign;
>  A.i = 10;
>  B = A;
>
>  // make changes to A
>  // should not affect B
>  A.i = 0;
>  printf("%d"\t,B.i); // should print 10
>
>  // make changes to B
>  // should not affect A
>  B.i = -10;
>  printf("%d",A.i);  // should print 0;
>
>  return 0;
>}
>
>output:
>
>0    -10
>
>I would like to copy the contents of A to B. Is there a way to implement a copy constructor and use the assignment operator to invoke it? Please demonstrate.
>
>Andrew
>
>


October 28, 2003
In article <bnm3f2$3af$1@digitaldaemon.com>, jhenzie@mac.com says...
>
>I could be wrong but I thought that A and B are references thus B = A makes B a
>reference to the
>same instance as is referenced from A, thus changes to A are reflected in B and
>visa versa.
>
>The assign created as B is no longer strongly routed and is now a candidate for garbage collection.
>
>The assigment operator does not appear to be overloadable probably because of
>reference
>semantics thus the necessity for dup or clone funtions within types that support
>such behavior.
>
>Hope that helps.
>
>Justin
>
>
>In article <bnlh1i$2c94$1@digitaldaemon.com>, Andrew Edwards says...
>>
>>Is overloading of the assignment opperator not allowed, overlooked, or simply undocumented? Currently, assigning A to B results in B becoming a pointer to A.
>>
>>ie
>>
>>class assign
>>{
>>  this() {}
>>  int i;
>>}
>>
>>int main()
>>{
>>  assign A = new assign;
>>  assign B = new assign;
>>  A.i = 10;
>>  B = A;
>>
>>  // make changes to A
>>  // should not affect B
>>  A.i = 0;
>>  printf("%d"\t,B.i); // should print 10
>>
>>  // make changes to B
>>  // should not affect A
>>  B.i = -10;
>>  printf("%d",A.i);  // should print 0;
>>
>>  return 0;
>>}
>>
>>output:
>>
>>0    -10
>>
>>I would like to copy the contents of A to B. Is there a way to implement a copy constructor and use the assignment operator to invoke it? Please demonstrate.
>>
>>Andrew
>>
>>
>
>
Correct, both A and B are references, try...

import c.stdio ;

class X
{
int x ;

this ( int x )
{
this.x = x ;
}

this ( X x )
{
this.x = x.x ;
}
}

void
main
(
char[][] args
)
{
X a = new X ( 5 ) ;
X b = new X ( a ) ; // Construct a copy of a and have b reference it

a.x++ ;

printf ( "\na.x = %d" , a.x ) ;
printf ( "\nb.x = %d" , b.x ) ;

return ;
}



October 28, 2003
> >>
> >>Is overloading of the assignment opperator not allowed, overlooked, or simply undocumented? Currently, assigning A to B results in B becoming a pointer to A.
> >>
> >>ie
> >>
> >>class assign
> >>{
> >>  this() {}
> >>  int i;
> >>}
> >>
> >>int main()
> >>{
> >>  assign A = new assign;
> >>  assign B = new assign;
> >>  A.i = 10;
> >>  B = A;
> >>
> >>  // make changes to A
> >>  // should not affect B
> >>  A.i = 0;
> >>  printf("%d"\t,B.i); // should print 10
> >>

This is not what will happen since variables are reference to classes...

And although this is not intuitive for a C++ programmer, I think this is intended to be that way...

> >>I would like to copy the contents of A to B. Is there a way to implement
a
> >>copy constructor and use the assignment operator to invoke it? Please demonstrate.
> >>

I would like to have an assignment operator (maybe := or <- or even .assign)
that could be used when we want to to copy the contents (and not make
a reference to the data).

OTOH, garbage collected reference make it easy to share objects without explicit need for memory managment and such...

Philippe


October 29, 2003
"Andrew Edwards" <edwardsac@spamfreeusa.com> wrote in message news:bnlh1i$2c94$1@digitaldaemon.com...
> Is overloading of the assignment opperator not allowed, overlooked, or simply undocumented?

It's not allowed. The semantics are set up so that there are no copy constructors or overloadable assignment operators.

There is, however, a .dup property that by convention creates a copy of the object it is called on.