View previous topic :: View next topic |
Author |
Message |
Sue D. Nymme
Joined: 26 Jul 2006 Posts: 13 Location: Philadelphia, PA, USA
|
Posted: Fri Jul 28, 2006 7:41 am Post subject: Static data inheritance problem |
|
|
Hello, I have a class hierarchy in which each class has some data that is constant for the class (but which varies from class to class). I have boiled it down to the following example program:
Code: |
import std.stdio;
class Parent
{
static char[] info = "Parent class";
this()
{
writefln(`Constructing a ?s. info is "?s"`,
this.classinfo.name, this.info);
}
void print()
{
writefln(`I am a ?s. My info is "?s"`,
this.classinfo.name, this.info);
}
}
class Child : Parent
{
static char[] info = "Child class";
}
void main()
{
Parent P = new Parent;
Child C = new Child;
P.print();
C.print();
}
|
The output I would like to see is:
Quote: |
Constructing a Parent. info is "Parent class"
Constructing a Child. info is "Child class"
I am a Parent. My info is "Parent class"
I am a Child. My info is "Child class"
|
The actual output I am getting is:
Quote: |
Constructing a Parent. info is "Parent class"
Constructing a Child. info is "Parent class"
I am a Parent. My info is "Parent class"
I am a Child. My info is "Parent class"
|
The base class methods seem to see only the base class static data.
If I copy the print() function from the base class into the child class, then C.print() does print
Quote: |
I am a Child. My info is "Child class"
|
But I would like to avoid that -- the whole point of inheritance is not to duplicate code!
Any help would be appreciated. _________________ "If anyone finds the above offensive, I am prepared not only to retract it, but also to deny under oath that I ever said it." -- Tom Lehrer |
|
Back to top |
|
|
pragma
Joined: 28 May 2004 Posts: 607 Location: Washington, DC
|
Posted: Fri Jul 28, 2006 7:53 am Post subject: |
|
|
Static members aren't placed in the class' v-table, so they're not inherited/overridden in the same way as members. In your code above, the reference to "this.info" is equivalent to "Parent.info".
Anyway, just wrap your reference to 'info' with a member instead, and all will be well. Constant strings are by definition stored statically, so we can do away with the 'static char[]' stuff and get right to it:
Code: |
class Parent
{
char[] info(){ return "Parent class"; }
this()
{
writefln(`Constructing a ?s. info is "?s"`,
this.classinfo.name, this.info);
}
void print()
{
writefln(`I am a ?s. My info is "?s"`,
this.classinfo.name, this.info);
}
}
class Child : Parent
{
char[] info(){ return "Child class"; }
}
|
_________________ -- !Eric.t.Anderton at gmail |
|
Back to top |
|
|
Sue D. Nymme
Joined: 26 Jul 2006 Posts: 13 Location: Philadelphia, PA, USA
|
Posted: Fri Jul 28, 2006 9:46 am Post subject: |
|
|
Thanks pragma. Works like a charm. _________________ "If anyone finds the above offensive, I am prepared not only to retract it, but also to deny under oath that I ever said it." -- Tom Lehrer |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|