Pointer to Object Instance
computer guy
Posts: 1,113
Lets say I have 3 objects (OBJA, OBJB and OBJC).
OBJA includes OBJB and OBJC
e.g.
OBJA
The problem is, OBJC needs to access a variables value in OBJB.
At the moment I am declaring the variable in OBJA and passing OBJB and OBJC a pointer to the variable.
Is there a better way to do this? Can you have a pointer to an object?
Thanks
OBJA includes OBJB and OBJC
e.g.
OBJA
OBJ OBJB : "OBJB" OBJC : "OBJC"
The problem is, OBJC needs to access a variables value in OBJB.
At the moment I am declaring the variable in OBJA and passing OBJB and OBJC a pointer to the variable.
Is there a better way to do this? Can you have a pointer to an object?
Thanks
Comments
So is this standard practice then?
Unlike C++ Spin does not allow you to take the address of an object and then pass that around to other objects so that they can access its methods and properties.
Of course if the data area in question more logically belongs in object B or C, then perhaps it makes sense to define it in B or C and provide a method to get it's address that can be called by A. That address can then be passed to the other object that needs it.
1) Define the data you want to share in some object.
2) Put all the data in a DAT section.
3) Give that object some PUB methods that set and get that data.
4) Include that object in any other objects you like with the normal obj syntax.
5) Have those objects use the shared data by calling the accessor methods.
6) For complicated structures the set and get methods will need to implement locks.
This relies on the fact that there is only ever one DAT section created in the final program no matter how many instances of the object are created. In contrast to VAR variables where every instances gets it's own copy.
You could, for example, have just a single get and stet method for your data object that takes a parameter indicating what is to be read or updated.
I haven't tried this, but what might work is declaring all your variables in a DAT block in a separate object, then in the CON block of that object declare constants equal to the address of the variables in the DAT block, and when in the main object, reference [pointers#varA] where pointers is the object where everything is stored. This would remove the possibility of accidentally writing over used memory space. Correct me if I'm wrong, please.
OBJA initialises the other objects and then sleeps (waitcnt) for long periods of time, only waking up to do small tasks.
OBJB is a Bluetooth driver object that holds a variable called "conState" which can have three values (0=disconnected, 1=connecting, 2=connected)
OBJC is an object that contains drawing routines for displaying icons and text on a uOLED-128-G1 screen. So it needs to know the value in "conState" so that is knows if it should display the BT logo.
There are other variables that OBJB and OBJC share. Such as the RSSI value.
I was hoping to get away from having pointer accessors/setters defined in both OBJB and OBJC, but it appears this is the only way.
OBJB will have "pointerForConState"
OBJC will have "setPointerForConState"
OBJA will use both methods to give OBJC the pointer from OBJB.
I think he meant using constants as address offsets which you can add to a base address to get/set the data (in a dat section - var sections reorganize things for you). You can pass addresses anywhere; just can't pass objects.
Just the offsets. Get/Set methods would be easier to read. Example:
There was a thread here in this Forum about Dynamic Linking of Objects (DOL link here)
Not simple but possible...
Enjoy!
Mie