Inheritance in JASS

Ayanami

칼리
Reaction score
288
Wanted to know how Inheritance exactly works in JASS. Let's say you have struct Parent and struct Child, I understand that the Child inherits instance members and methods only, right?

I'm confused about the constructor part. If you call [ljass]local Child c = Child.create()[/ljass], this calls Parent's [ljass].create[/ljass] method? Would that mean the variable c holds Parent's instance or Child's instance? I would like to know how exactly the constructor works for a Child & Parent.

Another question:

JASS:
struct Parent
endstruct

struct Child extends Parent
endstruct

function Test takes nothing returns nothing
    local Parent p = Child.create() // legal?
endfunction


If the above is legal, let's say I have a new scenario: struct Parent, struct Child1, struct Child2.

JASS:
struct Parent
    method print takes nothing returns nothing
        call BJDebugMsg("Hi")
    endmethod
endstruct

struct Child1 extends Parent
endstruct

struct Child2 extends Parent
endstruct

function Test takes nothing returns nothing
    local Parent p

    if GetRandomInt(0, 1) == 0 then
        set p = Child1.create()
    else
        set p = Child2.create()
    endif

    // anyway to check if p holds an instance of Child1 or Child2?
endfunction


And for the above scenario, would calling [ljass]p.print()[/ljass] result in an error?

Thanks.
 

Dirac

22710180
Reaction score
147
I'm almost certain that child structs have no allocation methods and take instead the parent's methods.
I advise you to avoid struct inheritance as it takes you nowhere plus you can't extend structs to other structs that extend array (which is really stupid)
 

Bribe

vJass errors are legion
Reaction score
67
1. Yes it is legal since Parent and Child share the same allocators.

2. [ljass]if p.getType() == Child1.typeid then[/ljass]
 

lep

Active Member
Reaction score
8
http://wc3c.net/vexorian/jasshelpermanual.html#extendstruct

1. Yes it is technically legal but since Parent and Child do not share the same allocators this is going to cause errors for you.
Uhm? They do.
JASS:
//Generated allocator of Parent
function s__Parent__allocate takes nothing returns integer
 local integer this=si__Parent_F
    if (this!=0) then
        set si__Parent_F=si__Parent_V[this]
    else
        set si__Parent_I=si__Parent_I+1
        set this=si__Parent_I
    endif
    if (this>8190) then
        return 0
    endif

    set si__Parent_type[this]=1
    set si__Parent_V[this]=-1
 return this
endfunction

//Generated allocator of Child2
function s__Child2__allocate takes nothing returns integer
 local integer this=s__Parent__allocate()
 local integer kthis
    if(this==0) then
        return 0
    endif
    set si__Parent_type[this]=3
    set kthis=this

 return this
endfunction


//Generated allocator of Child1
function s__Child1__allocate takes nothing returns integer
 local integer this=s__Parent__allocate()
 local integer kthis
    if(this==0) then
        return 0
    endif
    set si__Parent_type[this]=2
    set kthis=this

 return this
endfunction


But yeah, just read the [...] manual.
 

Bribe

vJass errors are legion
Reaction score
67
lep, you replied too quickly. I edited my post after I saw that child way extending parent.
 

Ayanami

칼리
Reaction score
288
So practically, this means Parent, Child1, Child2 all together can only have 8190 instances? That's kinda lame.
 

Bribe

vJass errors are legion
Reaction score
67
JASS:
struct parent[200000]


Of course this adds plenty of KB size to your map.
 

Dirac

22710180
Reaction score
147
So practically, this means Parent, Child1, Child2 all together can only have 8190 instances? That's kinda lame.
The way i see it this is the only way it makes sense, but after reading what lep posted i find struct extensions to other structs even more stupid.
There's always a way to avoid them
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
could you use a struct to return integers used as the arrays for other structs instances? essentially making it 8190^2 (67076100 instances)
 

Bribe

vJass errors are legion
Reaction score
67
First change your avatar, it gives me the creeps.

Second, not in the way you think. If you want big integers which can exceed 2^31 you can use BigInt created by Nestharus.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I know the question has been answered, but for questions like these, remember that you can always look at the JASSHelper Manual. http://www.wc3c.net/vexorian/jasshelpermanual.html#extendstruct

After showing a struct B that extends a struct A:

JASSHelper Manual said:
Internally, B.allocate() is actually calling A's constructor and B.destroy will also call B's deconstructor. If a base struct got a custom create method, the structs extending it will have to use it for allocate(). If the custom create method requires arguments, the allocate method for child structs will require the same arguments

There's a bit of Engrish in there, but you can pretty much get the gist of it.
 

Dirac

22710180
Reaction score
147
GFreak45 you still have a lot to learn about structs. Here i present you this example.
JASS:
struct Values
    
    integer number
    
endstruct

function Test takes nothing returns nothing
    
    local integer i = Values(6).number
    
endfunction
Is the same as
JASS:
struct Values
    
    integer number
    
endstruct

function Test takes nothing returns nothing
    
    local Values someValue = Values(6) // you can just write 6 here and it would work
    
    local integer i = someValue.number
    
endfunction
 

Dirac

22710180
Reaction score
147
Yes, to avoid interfaces use textmacros
to avoid inheritance, well, don't do it
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
whats the difference between .number/.create and all of the other .tags on the end of the set variable?
 

tooltiperror

Super Moderator
Reaction score
231
> Yes, to avoid interfaces use textmacros
I recommend you do not use textmacros for inheritance :)

I find that multiple inheritance is a very complicated and tricky concept, and I don't use it in any language. I do, however, support reopening structs (but Jass doesn't support this) and I think modules are great. You can just make a module replace a parent class and implement it, and change a few minor details.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top