Tech Tip: Pointer Notation Syntax: 4D vs. C
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: May 7, 2008
Pointer notation in 4D looks a bit different from the C style notation used in languages like C++. It can be tempting to assume the wrong function of a certain symbol, such as the arrow, based on familiarity with C++ in particular.
A side-by-side comparison with C++ may help to see the distinction. This assumes that objX is a C++ object of class type ObjectX.
4D Syntax | C++, Simple Variable | C++, Object Member | |
Referenced Variable | myInt:=12 | myInt=12; | objX.myInt=12; |
Declare Pointer | (Not required) | int *iPtr; | ObjectX *oPtr; |
Assign Reference | myPtr:=->myInt | iPtr=&myInt; | oPtr=&objX; |
Simple Dereference | ALERT(myPtr->) | cout << *iPtr; | cout << oPtr->myInt; |
Dereferenced Assignment | myPtr->:=13 | *iPtr = 13; | oPtr->myInt = 13; |
The arrow -> (dash plus wedge) symbol is your basic pointer dereferencing symbol in 4D, similar (but not identical) to the asterisk * in C and C++. In C++ the arrow is used as a contraction of pointer dereferencing and object member access: oPtr->myInt is equivalent to *(oPtr).myInt .
One subtle difference to note in 4D is assignment of the reference to the pointer, vs. assignment of a value to the variable deferenced by the pointer:
:=-> Assign pointer to point to the variable
->:= Assign a value to the dereferenced variable