Function Overload its possible?
BlitzMax Forums/BlitzMax Beginners Area/Function Overload its possible?
| ||
Hi guys! So Im trying to create to constructors for may objetc see: Type TObject List:TList Field x%, y% Function Create:TObject() Local in:TObject in = New TObject If Not List Then List = New TList List.AddLast(in) Return in EndFunction Function Create:TObject(x:Int=0, y:Int=0) Local in:TObject = Create() in.x = x in.y = y Return in EndFunction EndType Its possible with BMax? Because Im getting a "duplicate function" error. Thanks in advance |
| ||
Nope... no function or operator overloading in BlitzMax... You need to do something like this: SuperStrict Type TObject Global List:TList Field x%, y% Function Create:TObject() Local in:TObject = New TObject If Not List Then List = New TList List.AddLast(in) Return in EndFunction Function CreateSpecial:TObject(x:Int=0, y:Int=0) Local in:TObject = Create() in.x = x in.y = y Return in EndFunction EndType For Local i% = 0 To 10 TObject.CreateSpecial(Rand(100),Rand(100)) Next For Local o:TObject = EachIn TObject.List Print o.x Next |
| ||
what he said. |
| ||
Method New is automatically called whenever you create the object. It cannot take any parameters but it is useful for adding to lists etc. You can create the TList at the beginning of the program as a global variable - there is no need to check each time.Type TObject Global List:TList = New TList Field x%, y% Method New() List.AddLast(Self) EndMethod Function Create:TObject(x:Int=0, y:Int=0) Local in:TObject = New TObject in.x = x in.y = y Return in EndFunction EndType |
| ||
Yeah method New() is really clean to do this, so less code spared betwen my constructors :) Thanks! |