static data in a sub-class

Steve D whatchcallamit at weedline.com
Sun Dec 23 14:08:20 PST 2012


Thanks Ali, I'm still playing with your earlier suggestion of 
passing the array to A in a super() call from B.
But now... I dunno things are getting worse... it 'sometimes' 
works see below..

While we're chatting, I've slightly added to the earlier example 
to better replicate my actual code.

Let's say it's a game and the objects are creatures. class A is a 
monster and array hi represents it's high'est speed(s) over 
various terrains.
Class B is a SwiftMonster who's hi's range a bit higher to allow 
it to move more quickly than a std monster.

Both 'hi' speeds apply to all monsters of that class so it's 
better to be a class-level (static) array (defined once vs 
hundreds of copies).

the run function  run() is common to any monster so I have 
defined it in Monster and 'should' be able to inherit it for any 
monster.
Both Monster and SwiftMonster may be instantiated many times.

I don't want to duplicate the common 'run' functionality run() so 
just define it in the lowest Monster class and inherit it for 
others (there could be many monster types).

Do these requirements sound reasonable? I think they do given the 
promise of Object Orientation - I'm not really asking for 
anything complicated or to jump through hoops.

I just want to inherit the run function without it coming 
pre-packaged with the array of the class where it was declared 
(assume in some circumstances I don't even need a basic Monster)

So, ok I have to compromise a bit and started using your pass 
'hi' at constructor time and with some revised code ended up with 
this


     class Creature {
         abstract void move();
     }

     class Monster :Creature {
         static float[3] hi = 1;

         this(float[] x=null) {if (x) hi = x;}

         final void run() {  writefln("hi running at %s",hi);  }

         void move() { run(); }

     }

     class SwiftMonster : Monster {
         static float[3] hi = 2;

         this() {super(hi);}

         void move() { run(); }
     }


running thusly

void main()
{
     Creature a = new Monster();
     a.move();

     Creature b = new SwiftMonster();
     b.move();
}

gives...
'hi running at [1, 1, 1]'     <-- ok this seems to work fine
'hi running at [2, 2, 2]'     <-- ok this seems to work fine


But then when I try to process them in a loop of creatures (you 
would, in a game, right?) I get this

void main()
{
     Creature[2] creatures;
     creatures[0] = new Monster();
     creatures[1] = new SwiftMonster();
     foreach(c; creatures) c.move();
}

gives...
hi running at [2, 2, 2]       <-- WTF?
hi running at [2, 2, 2]

Why Ali? Why?
Why aren't things simple?
I even tried my original question code in C++ and it's the same 
there.
Are my requirements too darned demanding of OOP? Surely not...?
Also why isn't behavior consistent.

Why Ali? Why?   ;)

Cheers
Steve




More information about the Digitalmars-d-learn mailing list