Constructor and Destructor for Types
BlitzMax Forums/BlitzMax Programming/Constructor and Destructor for Types
| ||
After looking through the documentation and searching the forum i am still not sure. Is it true that BM does not have some kind of destructor and constructor which is automatically called when creating a type. I mean something like classes in C++ have. If so it would be nice to have them like a OnCreate and OnDestroy function for every type. So that this functions are automatically called when a type is created with new or when a type is destroyed by the garbagecollector. Mirko PS: OK, the OnCreate can be called by my programm after it creates a type using new, but the OnDestroy should occure when the type is killed by the gb, as i might not know how long the type is used. Still, having both would be nice. |
| ||
Those do exist, but you have to declare them and you'll have to specify what they should do when they are created/destroyed:Type test Method New() ' Constructor method Print "Hey, you've just created a new test-instance" End Method Method Delete() ' Destructor method Print "You've just destroyed a test-instance" End Method End Type a:test = New test ' Create a new instance of "test" a = Null ' Set "a" to point to nothing (so it can be destroyed at the next FlushMem) FlushMem ' Destroy all unreferenced objects |
| ||
On destroy can be don with a Delete method automatically caled by the gc. |
| ||
A few things to note: you can't have arguments in the 'constructor' (it's not really a constructor, in my opinion), and you can't call the destructor. Or you shouldn't be able to, anyways. If you want to have a pseudo-constructor, create a static function in the class and call it Create, that way it's pretty obvious what it does. For example, my entity classes have a static function Spawn that, well, spawns one of them at a given point. Probably already knew that, but I figured I'd mention it. |
| ||
Hey, thanx to all of you. I did not know that. Is it documentet somewhere or how do you know about New and Delete Methods? |
| ||
It's under the language reference for user defined types. |