Copying Types

BlitzMax Forums/BlitzMax Programming/Copying Types

z80jim(Posted 2006) [#1]
I have a parent and child type and each has a copy() method. I want the child copy() to call the parent copy() and then do it's own additional copying. I have something like this:

[CODE]
Type TParent
field age : int
method copy : TParent()
parent : TParent = new TParent
parent.age = age
return parent
end method
End Type

Type TChild extends TParent
field grade : int
method copy : TChild()
child : TChild = new TChild
child = TChild( super.copy() ) '<------- problem line
child.grade = grade
return child
end method
end Type
[/CODE]

The problem is in my TChild copy() the child that is assigned from the TChild typecast always comes out null.

Any help on why it doesn't work or another method for doing these kinds of copy() methods from extended types.


H&K(Posted 2006) [#2]
You cannot cast that way. You can cast a child to a parent, because it has all the fields, but not a parent to a child. (but Im not brill at this so dont take my word)

I have parent.CopyFields(OtherParent), which doesnt have a new, and I use that for children

Method RM_Set:TIntPair (P_X:Int=0,P_Y:Int=0)

	Self.F_X = P_X
	Self.F_Y = P_Y
	Return Self
	
End Method
'	-----------------------------
Function RF_Create:TIntPair (P_X:Int=0,P_Y:Int=0)

	Return New TIntPair.RM_Set(P_X,P_Y)
	
End Function
'	----------------------------
Method RM_Copy:TIntPair()

	Return TIntPair.RF_Create(Self.F_X,Self.F_Y)
		
End Method
'	----------------------------
Method SM_Copy(P_IntPair:TIntPair)
	
	Self.RM_Set (P_IntPair.F_X,P_IntPair.F_Y)
	
End Method
'	----------------------------
The first copy calls Creates which makes a new IntPair, (RM Return Method), the second copy calls Set witch just copies the fields (SM SetMethod) of the param into the object