Create an extended type from an existing type
BlitzMax Forums/BlitzMax Programming/Create an extended type from an existing type
| ||
Type a EndType Type b Extends a EndType Let's say I have an object of type a. I want to create a new object of type b from it. Is this possible? |
| ||
You can have:Type A Method CasttoB:B() 'create new object End method End type Sadly i've not had any success doing the following. It looks like max can't cast custom types... Type A Method CasttoB:B() Obj:B=B(Self) Return Obj End method End type |
| ||
http://www.blitzmax.com/Community/posts.php?topic=60010#669591 Long topic, but I edited the first post to reflect the solution The basic answer is as long as the NEW isnt inside the function you use to allocate the values for the first object, than yes. What I tend to do now, is have a "Set" method and a "create" function, then the extended type calls its own create, which calls both sets. Type TRGB 'A Colour Tri ' -------------------------------------------------------------------- Field F_Red :Int Field F_Green :Int Field F_Blue :Int ' -------------------------------------------------------------------- Method RM_Set:TRgb(P_Red:Int=0,P_Green:Int=0,P_Blue:Int=0) Self.F_Red = P_Red Self.F_Green = P_Green Self.F_Blue = P_Blue Return Self End Method ' -------------------------------------------------------------------- Function RF_Create:TRgb(P_Red:Int=0,P_Green:Int=0,P_Blue:Int=0) Return New TRgb.RM_Set (P_Red,P_Green,P_Blue) End Function ' -------------------------------------------------------------------- Method RM_CreateCopy:TRgb() Return TRgb.RF_Create (Self.F_Red,Self.F_Green,Self.F_Blue) End Method Etc EndTypeNow if I extned RGB I only have to override the RF_Create, then from the new RF_Create calls the new Set, and the super Set. (Sorry about the syntax, but I didnt think you would would mind) |
| ||
Sorry I don't get it, how type b is created from type a in the above example? I don't even see any reference to type b. I have been reading the linked post but this is really a very confusing thread! :) |
| ||
I don't even see any reference to type b Thats cos there isnt any ;) It how to prepare the ground.All its showing is that you need to keep the place you create the NEW instance seperate from the place you allocate its field values. Thatway you have a method you can call from any extended type. |
| ||
Got it! Thanks |
| ||
No, the point was that I don't want to copy all the data. It's not that big of a deal, I was just wondering. |
| ||
You dont have to copy all the data, cos you now have self.set and super.set What you cannot do is overload anyof them. I admit that as the first impuse is to create and alocate in the same Function, then often it needs a bit of work in the base type. |