pointer to different types

BlitzMax Forums/BlitzMax Beginners Area/pointer to different types

Jur(Posted 2008) [#1]
Is there a way to assign different types to a pointer. I would like to have one variable-pointer name, which would act like a container for different classes.

This doesn't work:

Global obj:Object=New Ta

If Ta(obj) Then Print "obj=Ta"
Print obj.a

Type Ta
Field a
EndType


I found a way to store my object in an array, but there is a problem of down casting to get them out. There would be needed many if statements to find a correct class and also with excessive casting, code become messy to my eyes.

Jure


Azathoth(Posted 2008) [#2]
Print Ta(obj).a


Jur(Posted 2008) [#3]
thanks. Still... casting is needed. It would be great if obj would behave like common Ta instance if it is defined like one. It is not very efficient to get a correct type if there are many possible types stored in obj.

My situation is like this: I have one main object and some data objects. My idea was to have a pointer within a main object which could "attach" any of this data objects. Then I would make many instances of the main type with diferent data objects attached. I could then loop through all main objects and run data types methods (with the same name) very easilly.
With casting needed to receive a type my idea lose its simplicity and efficency, so I will have to figure out something else. (And i am open to other ideas :)

Jure


Dreamora(Posted 2008) [#4]
1. With reflection you can get the type faster and use select

2. Write a common type that has all the functionality in and extend from that. then you can call the common types functionality and create an array of that common type
-> no casting needed

example of such a thing is the Blitz3D entity system with the common type "entity" that has position, rotation, scale and a few other things which is extended by Mesh, Light, Camera, Terrain etc to do more specialized things.