I thought I knew OOP, oops.

BlitzMax Forums/BlitzMax Programming/I thought I knew OOP, oops.


Is there no better way?


not that i have come across anyway. there were some things discussed a long time ago dealing with allocating a new block of memory and copying the memory around but i dont think it really worked well. plus, as objects get more complex and have fields that are in effect, more type instance pointers, even with copying memory those would still point to the same references. i think a pretty accepted way of handling something like this is having a clone method that returns a copy of the object instance. that way you have control over what is the same and what is different (ie whether you want any fields that are also objects to either be cloned or point to the same instance). also, something that can be implemented are create functions, one of which would create from an instance of the same type. a testType showing some of this would be
Type testType
	Field a:Int=0

	Function create:testType()
		Return New testType
	EndFunction

	Function createFromInstance:testType(other:testType)
		Return other.Clone()		
	EndFunction

	Method update()
		a:+1
	EndMethod

	Method Clone:testType()
		Local retval:testType=create()
		retval.a=a
		Return retval
	EndMethod
EndType

with options being
Local Original:testType=testType.create()
Local myCopy1:testType=Original.Clone()
Local myCopy2:testType=testType.createFromInstance(Original)