Problem getting pointer to function in a type
BlitzMax Forums/BlitzMax Programming/Problem getting pointer to function in a type
| ||
I'm having a problem printing (with DrawText) a function pointer. The function I want to print the pointer of is held within a Type. I am trying to reference it from within a function within a separate program file which is included. For example: 'This is program 1 Include "program2.bmx" Type mytype Field a:Int Function myfunction() 'do stuff End Function End Type 'other stuff happens too Global myf:mytype=New mytype myfunction2() End 'This is program 2 which is included: Function myfunction2() Local obj:mytype=myf DrawText String(Int(Int Ptr(Byte Ptr(obj.myfunction2))[0])),50,50 'something like this creates an error, saying myfunction2 is an int() End Function I have no idea if this code works, it's a gross approximation. But it basically sums it up... I'm trying to display a function pointer - ie the content of the function pointer itself - showing where it points, from within a function which is in an included program (#2), where the function I need to know the ptr to is in an instance of a type defined in the main program (or in some other included program). I can't tell what's wrong but it seems that blitzmax thinks the function pointer is an int() instead of `int() int` and can't be indexed. How can I get it to display that function pointer? |
| ||
myfunction needs to know what type to return if type casting is confusing you next time you have an error break down complex multi statment statements into each step at a time hope this clarifies it 'This is program 1 Include "test2.bmx" Type mytype Field a:Int Function myfunction:String(c:Int) Print "c="+c Return "hi!" End Function End Type 'other stuff happens too Global myf:mytype=New mytype myf.a=1 myfunction2() End 'This is program 2 which is included: Function myfunction2() Local obj:mytype=myf Print "myf.a="+myf.a+"obj.a="+obj.a Print "return="+String(obj.myfunction(2)) End Function |
| ||
Sorry, you misunderstood. I am not calling the function. I want the function pointer of the function. If BlitzMax is thinking that I am calling the function but I just haven't supplied () then that's where the confusion is. I need to get the pointer to the function, not call the function. I will try breaking it down and see if that helps, thanks for the tip. |
| ||
Local p:Byte ptr p=obj.myfunction Print "p="+Int(p) |
| ||
Well that sort of works. I actually found out what the problem was. I had assumed that each time you create a new instance of a type that contains a function, there is a new copy of the function kept along with the data. This is not the case. There is only ever one instance of the *function* held at a single memory location that is used by ALL instances of the type that contains the function. I guess the same thing also happens with methods except methods are somehow coded to allow access to local fields in the type. I wish we had method pointers! Also in order to get a function in a type to see fields in the type, they have to be made Global. Making them global means that if you create another instance of the type with the same Global fields, the Global of the previous instance is overwritten - or something like that, you can only have one Global for a given type! Realising this actually is requiring me to do a complete rewrite of my application :-( ... I had misunderstood how functions in types work. And it turns out I was actually seeing the correct function pointer, I just hadn't realized it remained the same value for all type instances. |
| ||
Functions are on Type Base, only methods are on type instance base. But they don't have "pointers" ( actually it are not pointers anyway but some sort of delegates / agents ) |