C# abstract class static field inheritance

cantrem picture cantrem · Aug 22, 2010 · Viewed 17.3k times · Source

I feel like I skipped a C# class or two, but here's my dilemma:

I have an abstract class from which I derive multiple child classes.

I know for sure that for each of the child classes I will have a constructor that needs a certain static object as a model and this object will be different for each of the child classes.

My first approach was to make a public static object in the abstract parent class and then, before I start creating any instances of the child classes, I would modify it for each of them, but it turns out that this way I actually make only ONE static object, for the abstract class, and each of it's child classes uses it.

How could I solve the problem?

To be more exact, here is the pseudocode:

The parent abstract class:

 abstract class AbstractClass
{
   static public ModelObject Model;
   ...
}

One of the child classes:

class Child : AbstractClass
{
    ...
    public Child()
    {
        this.someField = Model.someField;
    }
}

EDIT:

The Model needs to be a member of the "ModelObject" class, it should NOT be a singleton or anything else.

EDIT2:

To be even more exact, i chose this implementation for a game of chess: I have an abstract class for the chess pieces and the child classes represent the concrete pieces of the game: pawns, knights, et cetera.

The abstract class inherits from MeshMatObject, a class that represents generic 3d objects with the basic functionality, like rotations, meshes, materials, textures and so on and it defines abstract methods for chess game pieces, like GetPossibleMoves().

The Model object I was talking about above is a member of the MeshMatObject and, in my opinion, should be defined outside the class just once and then used for all the pieces. I mean: for example all the pawns have the same mesh and texture, so I don't see the point of giving a model as a parameter every time you want to make a pawn.

Answer

shf301 picture shf301 · Aug 22, 2010

You can get around the shared static field by making your Abstract class generic. Each generic class will get it's own copy of the static fields.

abstract class AbstractClass<T>
{
   static public ModelObject Model;
   ...
}

Then each child class will use a different instance of the static field.

class Child : AbstractClass<Child>
{
    ...
    public Child()
    {
        this.someField = Model.someField;
    }
}

It doesn't matter that AbstractClass doesn't reference the generic parameter. You are only using it to give each child class a unique instance of the base class.