Pointers to methods in Spin?
Duane Degn
Posts: 10,588
I think I remember from my C programming days that C (or C++) had pointers to methods (or where they called subroutines in C?). Is there a way of doing the same thing in Spin?
I think I understand both the sybol address '@' and object address plus symbol '@@' but I don't see any way of using a pointer to a method.
As I've mentioned in several other threads, I'm working on a menu program for Rayman's PTP. I've found it easier for me to keep track of the virtual buttons as "structures" in the DAT section.
I'm hoping I can add a pointer to the method the button envokes to this data structure.
I doubt there is an easy way to do this but I thought ask just in case it's not as hard as I think.
Duane
I think I understand both the sybol address '@' and object address plus symbol '@@' but I don't see any way of using a pointer to a method.
As I've mentioned in several other threads, I'm working on a menu program for Rayman's PTP. I've found it easier for me to keep track of the virtual buttons as "structures" in the DAT section.
I'm hoping I can add a pointer to the method the button envokes to this data structure.
I doubt there is an easy way to do this but I thought ask just in case it's not as hard as I think.
Duane

Comments
Usually this sort of thing is done by storing the index into a CASE statement with each case being a call to the appropriate method.
On the topic of cheating. The Object Address Plus can only be used with a variable not a pointer (at least that's the way I understand it). So I use this piece of code to come up number to use as my own version of object address plus. (diy stands for do it yourself)
Here are the variables it refers to.
DAT ledArrayButton0 byte 0, 7, 11, _White, _Red, _menuLedArray, " LED ", 0 menuStart word @ledArrayButton0, @ledArrayButton1, @timerAlarmButton0, @timerAlarmButton1, { } @otherMenuButton0, @otherMenuButton1, 0Now that I have a value for diyObjectAddressPlus, I can use it in this method to find the adjusted location of items listed in each group (here a group is a list of buttons for a particular menu).PUB MenuButtons | buttonLocation, localIndex localIndex := menuId ' menuId can be the location of any of the lists of menu buttons buttonLocation := 1 repeat while buttonLocation <> diyObjectAddressPlus buttonLocation := word[localIndex] + diyObjectAddressPlus if buttonLocation == diyObjectAddressPlus ' end my list with zero to identify the end. ' since I'm adding diyObjectAddressPlus to each pointer 'I look for its value instead of zero return PrintButton(buttonLocation) ' I can get away without using the @@ operator. localIndex += 2Which lets me access the many lists of data locations (the first being menuStart (as long as the lists are all in the same object)). This doesn't really have much to do with pointers to methods but I think it's pretty cool. I'm starting to understand the way to access parts of Propeller memory but I don't think I'm ready to try hacking method addresses into my code yet.I just discovered this tweak about getting around using the @@ operator and wanted to share it.
Thanks again Mike.
Duane