Copying objects vs. primitive type

muffles sgtmuffles at myrealbox.com
Sat Jun 30 17:43:25 PDT 2007


Ok, so since I'm in the process of learning D, I figured it could be helpful to make a template linked list class. Since D works by treating instances of objects as references, it was suggested by people in #D that I create a dup() method when I want a copy of an object of a class I create, since using the assignment operator will just have the new reference point to the same object as the old one. So I started writing a dup() method for my linked list when I ran into problem. When I copy the data in a node from the old list to its corresponding place in the new list, I'll only be copying references if what the linked list is templated for a class (if my understanding is correct). If my linked list is of a primitive type, the assignment operator will do what I want and dup() will give what I want, a second linked list with node that contain data equivalent to the corresponding nodes in the original list. But if the linked list is of an object, I still get a new list, but the data is just references to the objects that are also being referenced to by the nodes in the original list. How should I go about accounting for this difference in behavior?


More information about the Digitalmars-d-learn mailing list