Computer Science Canada Pointers |
Author: | smool [ Sat May 05, 2012 9:21 pm ] | ||
Post subject: | Pointers | ||
If I have the following:
Then there are now 2 instances of the class Person, but only 1 of them is being used as both person1 and person2 point to the same set of data. Thus, my outputs of person1 -> Name and person2 -> Name are both the same, "Hobbes". Is there anyway so that when I make pointer variables equal to each-other, that instead of pointing to the same data that it just recreates the data in 2 instances? So that I would have both person1 and person2 point to different instances of Person but have the same data? |
Author: | mirhagk [ Sat May 05, 2012 10:16 pm ] |
Post subject: | RE:Pointers |
Some languages allow cloning in certain instances, Turing does not. You will need to manually do it (and too bad there isn't operator overloading either) |
Author: | evildaddy911 [ Sun May 06, 2012 7:44 am ] |
Post subject: | RE:Pointers |
From looking at this, my guess is something like Var person2:^Person Person2->newperson(person1 -> name, person1 -> age) |
Author: | mirhagk [ Sun May 06, 2012 8:35 am ] |
Post subject: | RE:Pointers |
I'd suggest creating a method in the person class that creates an object identical to itself, and returns the new object. That way the caller of the object doesn't have to worry about the specifics of cloning the object (and it's a lot less to worry about, and a lot less chance of missing something). |
Author: | evildaddy911 [ Sun May 06, 2012 9:04 am ] |
Post subject: | RE:Pointers |
yeah, that works pretty well... *HINT* use a procedure for that: proc duplicate (var ...) |
Author: | smool [ Sun May 06, 2012 9:34 am ] |
Post subject: | RE:Pointers |
Alright, thanks guys. |