www.digitalmars.com         C & C++   DMDScript  

D - operator overloading: assign(A a);

reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
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
Oct 28 2003
next sibling parent reply jhenzie mac.com writes:
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
Oct 28 2003
parent reply Juan C <Juan_member pathlink.com> writes:
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 ; }
Oct 28 2003
parent "Philippe Mori" <philippe_mori hotmail.com> writes:
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
Oct 28 2003
prev sibling parent "Walter" <walter digitalmars.com> writes:
"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.
Oct 28 2003