I'm having some difficulty with function pointers, and quite possibly this might be a limitation of BlitzMax as a language/compiler.
I have an array of Objects in which I will be storing custom types of various kinds. Obviously this means that once the objects are in the array, you do not know what type of object they really are.
I have an array of Kinds, which is just an integer value referring to what kind of object it originally was.
The simple (but inefficient) solution for getting the right type of object out of the array is to do a Select-Case based on the Kind[Offset] for Objects[Offset].
This of course works. Also *if I know* the situation in which a given object type is to be used, such as that I need a TPixmap object, I can of course just cast it like TPixmap(Objects[Offset]). Becauase I am explicitly casting it within the sourcecode, the compiler knows that I can then call any functions or methods within TPixmap and it will be valid, e.g. TPixmap(Objects[Offset]).ReadPixel(10,5). Because the compiler knows the allowed methods of TPixmap objects, and knows I am casting it to a TPixmap, it lets it be compiled.
On the other hand, there are situations where I do not know the type of object and want to just, for example, go through all the objects stored and call a method that they all share. For example each of my custom types has a PrintSelf() method that prints details about its fields. I want to be able to run through the objects array, *not* using the kinds array, and call the PrintSelf() method of each object. To do this, what I am trying to do is have a Function Pointer for each object. This function pointer points to a Convert() function within the custom type. So all I should have to do is go through the objects and call, for example, ConversionFunctions[Offset](Objects[Offset]).PrintSelf()
However, the compiler will not compile the sourcecode. It says the PrintSelf() is not defined. I think that this is because it doesn't know what type of object it is dealing with and so is either looking in the Object() type and not finding that method, or it knows that it just can't tell what the type will be so it can't verify that it's a valid method. Is that what's happening?
Is there some way I can call the ConversionFunction (which would simply =Convert() function within a given type, to which you pass an object), and get it to convert to a specific object type, and then also call the method within that type, and have the compiler say it's ok?
I am wondering if it's because the array of Conversion Functions is defined as Converters:Object(Obj:Object)[], which means that no matter what type is returned from Convert() it will change it into just an Object and so won't return the actual type of object? How could I store the functions in one array even though they each return different types?
e.g. A Convert() function might look like:
[code]
Function Convert:MyType(Obj:Object)
Return MyType(Obj)
End Function
[Code]
Am I right in thinking that storing this in an array of function pointers that return Objects, cancells out the type conversion?
How can I make this work, so I can call Converters[Offset](Objects[Offset]).PrintSelf()
???
I have an array of Objects in which I will be storing custom types of various kinds. Obviously this means that once the objects are in the array, you do not know what type of object they really are.
I have an array of Kinds, which is just an integer value referring to what kind of object it originally was.
The simple (but inefficient) solution for getting the right type of object out of the array is to do a Select-Case based on the Kind[Offset] for Objects[Offset].
This of course works. Also *if I know* the situation in which a given object type is to be used, such as that I need a TPixmap object, I can of course just cast it like TPixmap(Objects[Offset]). Becauase I am explicitly casting it within the sourcecode, the compiler knows that I can then call any functions or methods within TPixmap and it will be valid, e.g. TPixmap(Objects[Offset]).ReadPixel(10,5). Because the compiler knows the allowed methods of TPixmap objects, and knows I am casting it to a TPixmap, it lets it be compiled.
On the other hand, there are situations where I do not know the type of object and want to just, for example, go through all the objects stored and call a method that they all share. For example each of my custom types has a PrintSelf() method that prints details about its fields. I want to be able to run through the objects array, *not* using the kinds array, and call the PrintSelf() method of each object. To do this, what I am trying to do is have a Function Pointer for each object. This function pointer points to a Convert() function within the custom type. So all I should have to do is go through the objects and call, for example, ConversionFunctions[Offset](Objects[Offset]).PrintSelf()
However, the compiler will not compile the sourcecode. It says the PrintSelf() is not defined. I think that this is because it doesn't know what type of object it is dealing with and so is either looking in the Object() type and not finding that method, or it knows that it just can't tell what the type will be so it can't verify that it's a valid method. Is that what's happening?
Is there some way I can call the ConversionFunction (which would simply =Convert() function within a given type, to which you pass an object), and get it to convert to a specific object type, and then also call the method within that type, and have the compiler say it's ok?
I am wondering if it's because the array of Conversion Functions is defined as Converters:Object(Obj:Object)[], which means that no matter what type is returned from Convert() it will change it into just an Object and so won't return the actual type of object? How could I store the functions in one array even though they each return different types?
e.g. A Convert() function might look like:
[code]
Function Convert:MyType(Obj:Object)
Return MyType(Obj)
End Function
[Code]
Am I right in thinking that storing this in an array of function pointers that return Objects, cancells out the type conversion?
How can I make this work, so I can call Converters[Offset](Objects[Offset]).PrintSelf()
???