D - operator overloading: assign(A a);
- Andrew Edwards (31/31) Oct 28 2003 Is overloading of the assignment opperator not allowed, overlooked, or
- jhenzie mac.com (13/44) Oct 28 2003 I could be wrong but I thought that A and B are references thus B = A ma...
- Juan C (28/85) Oct 28 2003 Correct, both A and B are references, try...
- Philippe Mori (10/39) Oct 28 2003 This is not what will happen since variables are reference to classes...
- Walter (6/8) Oct 28 2003 It's not allowed. The semantics are set up so that there are no copy
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
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
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...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 ; }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
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...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 10aI would like to copy the contents of A to B. Is there a way to implementI 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... Philippecopy constructor and use the assignment operator to invoke it? Please demonstrate.
Oct 28 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.
Oct 28 2003