Pointers and references: Difference between revisions

Content added Content deleted
(adding sas)
Line 605: Line 605:


(The OCaml web-site provides this [http://caml.inria.fr/resources/doc/guides/pointers.en.html page] on the subject.)
(The OCaml web-site provides this [http://caml.inria.fr/resources/doc/guides/pointers.en.html page] on the subject.)

=={{header|ooRexx}}==
In ooRexx, all values are ''references'' to an object. These references are kind of like pointers in C/C++, but you cannot perform arithmetic on them, nor can you directly dereference pointers or take the address of something. All objects are always accessed through references - you create one by sending a NEW message to a class object, which returns a reference; and you access an object's methods using the ~ operator, which takes a reference to the object as its left operand.

If the object that a reference is pointing to is mutable (i.e. it has methods that allows changing of its internal variables), then it is possible to modify that object's state through the reference; and someone else who has a reference to the same object will see that modification. (Note: this does not change the reference itself, just the object that it points to.) Let us consider a simple class of mutable object:

<lang oorexx>
::class Foo
::method init
expose x
x = 0
::attribute x

::routine somefunction
a = .Foo~new -- assigns a to point to a new Foo object
b = a -- b and a now point to the same object
a~x = 5 -- modifies the X variable inside the object pointer to by a
say b~x -- displays "5" because b points to the same object as a
</lang>

ooRexx is call-by-value. When passing arguments, you are passing object references by value. There is no such thing as "passing an object" as objects are not values in the language. As noted above, if the object that the reference is pointing to is mutable, then it is possible to modify that object's state through the reference. Then if the calling function has a reference to the same object, they will see that modification through the reference. So if you want to reflect changes in an argument back to the caller, one thing that you can do is wrap the argument as an attribute of an object, then modify the object through the reference.



=={{header|PARI/GP}}==
=={{header|PARI/GP}}==