
-----------------------------------
smool
Sat May 05, 2012 9:21 pm

Pointers
-----------------------------------
If I have the following:


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?

-----------------------------------
mirhagk
Sat May 05, 2012 10:16 pm

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
Sun May 06, 2012 7:44 am

RE:Pointers
-----------------------------------
From looking at this, my guess is something like

Var person2:^Person
Person2->newperson(person1 -> name, person1 -> age)

-----------------------------------
mirhagk
Sun May 06, 2012 8:35 am

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
Sun May 06, 2012 9:04 am

RE:Pointers
-----------------------------------
yeah, that works pretty well...
*HINT*
use a procedure for that:
proc duplicate (var ...)

-----------------------------------
smool
Sun May 06, 2012 9:34 am

RE:Pointers
-----------------------------------
Alright, thanks guys.
