static data in a sub-class
Steve D
whatchcallamit at weedline.com
Sun Dec 23 14:53:10 PST 2012
> class Monster :Creature {
> static float[3] hi = 1;
>
> this(float[] x=null) {if (x) hi = x;}
>That is a bad idea. x is passed to a single Monster object but
>the 'hi' static member of the whole Monster class gets affected.
>Just because there is a SwiftMonster, all Monsters become swift.
Yep I know and that's what I was trying to avoid with my original
(simplistic) version. I originally just wanted the inherited
function to be compiled in the context of it's current
environment (class B/SwiftMonster) rather than being linked to
array of class where declared.
Ah well, I think you have the solution which I haven't tried just
yet, so thanks in advance (and thanks for your patient replies).
While you where typing I tried it in python just for laughs..
class Creature: # not required, but just to be complete
def move():
pass
class Monster(Creature):
hi = [1,1,1]
def run(self):
print "hi running at",self.hi
def move(self):
self.run()
class SwiftMonster(Monster):
hi = [2,2,2]
def move(self):
self.run()
if __name__ == '__main__':
a = Monster()
a.move()
b = SwiftMonster()
b.move()
print
creatures = [Monster(),SwiftMonster(),SwiftMonster()]
for c in creatures:
c.move()
print id(c.hi)
hi running at [1, 1, 1]
hi running at [2, 2, 2]
hi running at [1, 1, 1]
28612448
hi running at [2, 2, 2]
28639280
hi running at [2, 2, 2]
28639280
(these id's are just to check that they're class-level arrays
(not object) which they seem to be.
See? Python eh? pseudo-code that just works! See my requirements
really aren't too demanding or impossible after all! :)
Thanks for now Ali, will get to trying your latest offering which
I'm sure will work.
Cheers
Steve
More information about the Digitalmars-d-learn
mailing list