An array of a user defined type with a constructor
BlitzMax Forums/BlitzMax Beginners Area/An array of a user defined type with a constructor
| ||
I've probably just missed it but, say you setup a type with a static method constructor egType TTest Field x:Int Function Create:TTest(x:Int) Test:TTest=New TTest Test.x=x Return Test End Function End Type I know you can go: Local testArray:TTest[] testArray=New TTest[10] Followed by a loop calling the constructer but that seems pretty wasteful on a few levels. Is there any way to set it up like: Local testArray:TTest[] testArray=New TTest.[10].Create(3) ...but obviously something that compiles. I've checked the docs but can't seem to find it. |
| ||
How about a Function to create the array for you.Type TTest
Field x:Int
'return an array of types
Function CreateTTestArray:TTest[]( count, x:Int )
local ar:TTest[count]
for local a=0 until count
ar[a]=TTest.Create(x)
next
return ar
endfunction
Function Create:TTest(x:Int)
Test:TTest=New TTest
Test.x=x
Return Test
End Function
End Type
'Call function to create an array.
local n:TTest[] = TTest.CreateTTestArray(20, 3)
print n[10].x |
| ||
| That's a very clever/nice solution, thank you. If there is no inbuilt way to do this otherwise, I'll do it that way permanently. Actually, removing Test:TTest=New TTest and altering Self should remove the other concern I have with using a loop. Does max also lack overriding methods/functions? That would be perfect for this. |
| ||
You can override methods and functions:
Type Animal
Field x = 320
Field y = 200
Method MakeNoise ()
Print "Thwwbbpppttt" ' Default sound...
End Method
End Type
Type Dog Extends Animal
Method MakeNoise ()
Print "Woof"
End Method
End Type
Type Cow Extends Animal
Method MakeNoise ()
Print "Moo"
End Method
End Type
a:Animal = New Animal
a.MakeNoise
Print a.x
Print a.y
d:Dog = New Dog
d.MakeNoise
Print d.x
Print d.y
c:Cow = New Cow
c.MakeNoise
Print c.x
Print c.y
|
| ||
| that's funny.. I was just earlier playing with some OOP, and I made an animal type which extended into a cow and a dog.. I did the "moo!" and "meeeooow!" methods too.. (it was funny to me anyways.. :p) |
| ||
| Pretty weird! Weirder still is that your dog said "meeeoow"... must be a Max bug. |
| ||
| lol.. sorry... I meant to write a cat.. LOL |
| ||
| Sorry, I meant overloading not overriding. Thanks for your help everyone. |