HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Extending structs question

07-10-2008, 01:27 AM#1
Ammorth
I've never really dealt with code like this, so thats why I'm asking questions.

I'm working on a system that starts with a base object, and then other objects extend the base:

Collapse pseudo code:
struct Object
    real x
    real y
    real z

    method create ...
    endmethod

    method onDestroy ...
    endmethod
endstruct

struct Circle extends Object
    real radius

    ...
endstruct

struct Square extends Object
    real xsize
    real ysize

    ...
endstruct

Now, I'm hoping that I can then make a function like so:

Collapse JASS:
function DoStuff takes object o returns nothing
    // the function will take either a square struct or a circle struct and do whatever with it, based on which it is.

I know you can do this with interfaces, but I can't specify methods within interfaces. I want to have base methods that all objects will use and then allow the user to create structs that extend the base, and add their own methods to each object type. Is this possible?
07-10-2008, 01:31 AM#2
grim001
Collapse JASS:
private interface ObjectInterface
endinterface

struct Object extends ObjectInterface
endstruct

struct UserObject extends Object
endstruct
07-10-2008, 01:50 AM#3
Ammorth
I think I get what you mean. I try and implement it and see how it goes.
07-10-2008, 03:33 AM#4
Pyrogasm
Collapse JASS:
function DoStuff takes object o returns nothing
   if o.getType() == Circle.typeId then
       call BJDebugMsg("Circle!")
   elseif o.getType() == Square.typeId then
       call BJDebugMsg("Square!")
   endif
endfunction
07-10-2008, 09:54 AM#5
Anitarf
There are two ways to specify common methods. With interfaces, you only specify the format of the method (it's name, what it takes and returns) and then each struct extending that interface can have a different method following that format. With structs, on the other hand, you write the entire method, not just it's format, in the parent struct, so it's the same for all structs that extend it.