• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Stub - Parent - Child - Thistype - Struct Problem

Status
Not open for further replies.
Hello guys there!

I am having a small problem...

JASS:
struct Parent
   static thistype array instances[15]

   stub method childOnly ...

   static method call
      call (run the other method of the current struct, NOT parent)
   endmethod

endstruct

struct Child extends Parent

   public method test ...
      call THIS_STRUCT_NAME.instances[0].call()
   endmethod

endstruct
Why does it call the method "childOnly" of the parent, instead of the child's method?

Edit:
What I want:

A parent struct
- This struct has a few stub methods the child will get
- The parent has a static method which will also be given to the child
-> In this method it referes to another static method of thistype (the struct itself)

A child struct
- Which extends the parent
- Which has a few new methods (stub overwrite)
- Which has a few parent methods (like the static one, without needing to rewrite in child)

even more explaination:
basicly I have 4 methods: 1 static and 1 normal in each struct (2 structs). The child struct also has the static, but also an own normal method. The static referes to the current struct normal method. But that doesn't work.
 
Last edited:
Level 21
Joined
Aug 21, 2005
Messages
3,699
But a static method is bound to the struct's type, while a stub method is bound to an instance of the struct...

Is this what you want to do?

JASS:
struct A
stub method bla ...
static method Bla ...
    local A a = B.create()
    call a.bla() // will display "Bla"
endmethod
endstruct

struct B extends A
    stub method bla ... display "Bla"
endstruct
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
That makes no sense.

Parent.call() is a static method. It cannot possibly call a method of the *current struct* because there *is* no current struct.

Maybe post your entire code instead of trying to make up an obscure example because I really don't see what you try to do.

Are you perhaps looking for something like:
JASS:
local A a = B.create()
if a.getType() == B.typeid then
    call BJDebugMsg("Struct a of static type A is of dynamic type B...")
endif
 
Level 11
Joined
Feb 22, 2006
Messages
752
First of all, you cannot override static methods. If you try to do something like this:

JASS:
struct A
    static method foo takes nothing returns nothing
    endmethod
endstruct

struct B
    static method foo takes nothing returns nothing
    endmethod
endstruct

You'll get a compile error.

Secondly, obviously your thing won't run the child's method because you haven't defined it yet. A simple solution to your problem would be:

JASS:
struct Parent
   static thistype array instances[15]
   stub method childOnly ...
endstruct
struct Child extends Parent
   method childOnly ...
   public method test ...
      call Parent.instances[0].childOnly()
   endmethod
endstruct

This will call the childOnly() method of the struct type of whatever instance is held by Parent.instances[0]. So if the instance was created using Parent.create(), it will call Parent.childOnly(); if the instance was created using Child.create(), it will call Child.childOnly().
 
I don't want to overwrite statics, I just want the childs to have them, too!
(So I don't need to have to type the same thing for them)

So the thing is: Childs will NOT have static vars?

And WHY isn't there a keyword for the current struct?

I mean, even the compiler needs to save the name of the current struct being opened. It would be SUCH an awesome method.

Oh btw, I want an thing that says inheritable.
( public inheritable static blah.. )
 
I think you can typecast directly from parent to child too, AKA

JASS:
struct Parent
    stub method test takes nothing returns nothing
        call BJDebugMsg("Parent")
    endmethod
endstruct
struct Child extends Parent
    method test takes nothing returns nothing
        call BJDebugMsg("Child")
    endmethod
endstruct

local Parent p = Parent.create()
call Child(p).test() //outputs "Child"
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
How can a static method be inherited? A static method is supposed to be called without providing any instance data.

Parent.func() is a static method and it does not take any object, it's just part of "being a Parent".
Child.func() cannot exist because it is already defined by Parent. As soon as you start slicing, there's no way you can figure out which static method you wish to call.

If you need static methods to be virtual, you're doing something wrong design-wise.
 
Urgh. His problem is this...
JASS:
struct A
    method doA takes nothing returns nothing
        //Need to work out which type of struct this was called from
    endmethod
endstruct
struct B extends A
endstruct
struct C extends A
endstruct

local B b = B.create()
local C c = C.create()

call b.doA() //How can the method tell that the instance that called the method was a B, a C, or an A?
call c.doA()
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
doA should not ever know specific things about its children. The method should not be able to tell which child type is calling it, because a parent does not know anything about its children. Only children know something about their parents. Anything else is a sign of bad design. When you need to do something like that, you should use stub methods.

JASS:
struct A
    stub method doA takes nothing returns nothing
        call BJDebugMsg("A")
    endmethod
endstruct

struct B extends A
    stub method doA takes nothing returns nothing
        call BJDebugMsg("B")
    endmethod
endstruct
 
Guys, you don´t get it!

what I basicly mean is that I need to refer to a static function inside a non-static method in a struct which extended / is a children of another struct, which basicly contains these static methods.

I need the same methods, but added to the children, because I can NOT use the parents method.

And what does SUPER do?


Edit: So I was asking to the "CURRENT_STRUCT_NAME" thing, because thistype is THE parent type on child creation, not the child name.
 
Status
Not open for further replies.
Top