passing pointers
BlitzMax Forums/BlitzMax Programming/passing pointers
| ||
if I have a Ptr, how can I pass it to a function by the original reference? that's not very clear.. :) here's some code which works except for the "other" method where I want to pass the Ptr I have. I get the following error: Variable for 'Var' parameter is not of matching type SuperStrict Local v:Float = 10 Local i:t = New t i.store v i.add Print v Type t Field value:Float Ptr Method store( v:Float Var ) value = Varptr v End Method Method add() value[0] :+ 1 End Method Method other() Local i:t = New t i.store value End Method End Type |
| ||
Well the code doesn't make sense since there's a type error: you are trying to pass a Float Ptr to a parameter expecting a Float. You can get it to compile by changing the line in `other` to: i.store value[0] `value[0]` is a Float, and can thus be passed to where one is expected. This has nothing to do with whether `Var` is used. The whole point of `Var` is to completely conceal the use of pointers as an implementation detail, so there's no interaction with `Ptr` types. Doing this gives the new `t` instance a pointer into the old one... if that's what you were actually trying to do. |
| ||
"value" is a Float Ptr. store() accepts a Float. They are not the same type. You probably want to do something like this : SuperStrict Framework brl.standardio Local v:Float = 10 Local i:t = New t i.store Varptr v i.add Print v Type t Field value:Float Ptr Method store( v:Float Ptr ) value = v End Method Method add() value[0] :+ 1 End Method Method other() Local i:t = New t i.store value End Method End Type |
| ||
???SuperStrict Local v:Float = 10 Local i:t = New t i.store v i.add Print v Type t Field value:Float Ptr Method store( v:Float Var ) value = Varptr(v) End Method Method add() value[0] :+ 1 End Method Method other() Local i:t = New t i.store(value[0]) End Method End Type |
| ||
man, I was thinking ..[0] just returned the value. so then, both versions work, Brucey's is probably more proper, right as it indicates what's going on? while using Var is cleaner as I don't need the Varptr when calling... hmmm thanks! |
| ||
As Yasha says, Var hides all the pointer stuff away so you don't need to know what's going on :-) |
| ||
>>Brucey's is probably more proper, right as it indicates what's going on? not necessarily but it's what you "think" it is. |