Pointers
Author |
Message |
smool
|
Posted: Sat May 05, 2012 9:21 pm Post subject: Pointers |
|
|
If I have the following:
Turing: |
class Person
export NewPerson, Age, Name
var name : string
var age : int
proc NewPerson (name_ : string, age_ : int)
name := name_
age := age_
end NewPerson
fcn Age : int
result age
end Age
fcn Name : string
result name
end Name
end Person
var person1, person2 :^Person
new person1
new person2
person1 -> NewPerson ("Calvin", 6)
person2 := person1
person2 -> NewPerson ("Hobbes", 6)
put person1 -> Name
put person2 -> Name
|
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? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
mirhagk
|
Posted: 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) |
|
|
|
|
|
evildaddy911
|
Posted: 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) |
|
|
|
|
|
mirhagk
|
Posted: 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). |
|
|
|
|
|
evildaddy911
|
Posted: 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 ...) |
|
|
|
|
|
smool
|
Posted: Sun May 06, 2012 9:34 am Post subject: RE:Pointers |
|
|
Alright, thanks guys. |
|
|
|
|
|
|
|