C# inheritance problem
Author |
Message |
mirhagk
|
Posted: Wed Nov 17, 2010 3:02 pm Post subject: C# inheritance problem |
|
|
If I have a class with a static variable, when another class inherits from it, does it get its own copy of it, or use the parent class' variable. Say for instance i have a parent class of enemies, and classes that inherit from it such as zombie, skeleton etc. I only want one copy of the texture for each class, so I want it to be static in all of them, but I need to define it in the parent class so it can use it in a draw method.
So can they each have their own copy of the parent's static variable or does each instance need it's own pointless copy of the same variable? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TerranceN
|
Posted: Wed Nov 17, 2010 3:44 pm Post subject: RE:C# inheritance problem |
|
|
The child class will access the parent's static variable.
Every instance is going to have to have a copy of that texture variable, but it is only a pointer, so it's not like it is going to effect performance in any noticeable way. |
|
|
|
|
|
mirhagk
|
Posted: Wed Nov 17, 2010 10:11 pm Post subject: RE:C# inheritance problem |
|
|
I guess... sad face. Maybe I'll store an array of textures and have them each just keep an eight bit unsigned int of which one they are.
Also when inheriting from a class that has a contructor with arguments, how do I pass arguments from the inherited class to the base class |
|
|
|
|
|
TerranceN
|
Posted: Thu Nov 18, 2010 3:49 pm Post subject: Re: C# inheritance problem |
|
|
How is that any better then having each instance have a Texture variable that in the constructor is set to point to the texture you want (It will only point to the texture, not copy it, because in C# all classes are reference types)?
As for the constructor, you can just use : Base() to send values to the parent's constructor. Like this
code: |
class A
{
int num;
public A(int newNum)
{
num = newNum;
}
}
class B : A
{
int num2;
public B(int newNum, int someOtherNum)
: base(newNum)
{
num2 = someOtherNum;
}
}
|
|
|
|
|
|
|
mirhagk
|
Posted: Thu Nov 18, 2010 8:48 pm Post subject: RE:C# inheritance problem |
|
|
okay thanks. And It's better because it's slightly less memory. And it'll just me annoying copying them all into the variables
but I got it all worked out, it's all good now. plus the array let's the parent function know about all the textures even if child functions don't exist |
|
|
|
|
|
|
|