type : copy an occurence

BlitzMax Forums/BlitzMax Beginners Area/type : copy an occurence

hub(Posted 2006) [#1]
Hi !
Is there a method to copy an occurence of a type to another (without copy field by field).
Thanks !

See this :

Strict

Global My_pixels_list:Tlist = CreateList()

Type TPixel
	Field x
	Field y
	
	Function Create:TPixel (x:Int , y:Int)
		
		Local p:TPixel = New TPixel
		
		p.x = x
		p.y = y
		
		ListAddLast My_pixels_list , p
		
		Return p
		
	End Function
	
	Function Copy:TPixel (p:TPixel)
		
		Local e:TPixel
		
		e = p ' <--- i just want copy p

		e.y = p.y + 10 ' <---- but p is also modified, in fact p.y = e.y, so e isn't a copy of p.
		
		ListAddLast My_pixels_list , e
		
		Return e
		
	End Function
	
	Function Draw_all ()
		
		SetColor 255,255,255
		For Local p:TPixel = EachIn My_pixels_list
			Plot p.x , p.y
			DebugLog "x=" + p.x
			DebugLog "y=" + p.y
		Next
		
	End Function
	
End Type

Graphics 800 , 600

Local p1:TPixel = TPixel.Create (100 , 100)
Local p2:TPixel = TPixel.Copy (p1)

While Not KeyDown(KEy_ESCAPE)
	TPixel.Draw_all()
	Flip
Wend



Diablo(Posted 2006) [#2]
i'm not sure how safe this is..
SuperStrict

Type TType
	Field ID$ , x% , y%
	
	Function Copy:TType(other:TType)
		
		Local cu:TType = New TType
		
		MemCopy(cu , other , SizeOf(TType) )
		
		Return cu
		
	End Function
End Type

Local a:TType = New TType
a.x = 10
a.y = 100
a.ID = "ONE"
Print "A"
Print a.ID
Print a.x + ", " + a.y
Print "Copying to B"
Local b:TType = TType.Copy(a)
Print "B"
Print b.ID
Print b.x + ", " + b.y
Print "Changing B..."
b.x = 20
b.y = 200
b.ID = "TWO"
Print "A"
Print a.ID
Print a.x + ", " + a.y

Print "B"
Print b.ID
Print b.x + ", " + b.y


but it seems to work, mabey someone with better lowlevel knowlage can help more?


Dreamora(Posted 2006) [#3]
Do not use this method!
This will bypass the GC and result in a NULL reference error when A is freed!


QuietBloke(Posted 2006) [#4]
will it ?

I would have thought this particular example would work. The copy creates a new instance of TType. Then splats the contents with the contents of the supplied TType.
Surely the GC is fully aware of this new Object ?

I would have thought as long as your type doesnt contain object pointers this will work. If your Type does contain an object pointer then the memcopy will copy the pointer over but this will go unnoticed by the GC and this will cause problems.

Anyway.. Whether it works or not I would personally just write the Copy function to create a new instance and copy over the fields. Otherwise at some point in the future I might decide to add a new field which contains a string or another type and BAM!.. things will start going weird and I'd end up wondering why.


Diablo(Posted 2006) [#5]
Anyway.. Whether it works or not I would personally just write the Copy function to create a new instance and copy over the fields. Otherwise at some point in the future I might decide to add a new field which contains a string or another type and BAM!.. things will start going weird and I'd end up wondering why.

Samehere.


Dreamora(Posted 2006) [#6]
No, only new is raising the reference count of the object. Without that, you will still have a reference, but the data behind it won't exist anymore.

Going the Clone method way is the much more stable and correct way and how it is done normally as well. Most are just "too lazy" to write once a clone method for each class they create. :-) (I've one in every class I instance)