Basically what I am trying to do here is make an ID for each derived type of Shape. ( Square is 1, Circle is 2, etc.) How can I make a static member variable that has polymorphic capabilities? How would I create the getters and setters?
class Shape {
public:
Shape() {}
static int ID;
};
class Square : public Shape {
public:
Square() {}
};
class Circle : public Shape {
public:
Circle() {}
};
class Person {
public:
int shape_type_ID
Shape* ptr;
Person(){}
};
int Shape::var{ 5 };
Is this a copy of this question? How to initialize `static` member polymorphically
EDIT: In my current design, each instance of Person contains a pointer ( I am not sure about the type of pointer ) that points to some Shape object. I want to restrict each Person object to only being able to reference one derived type of Shape.
E.g. Person 1’s pointer can only reference Circles, Person 2’s pointer can only reference Squares, Person 3’s pointer can only reference Triangles.
But I want 1 single Person class with 1 type of pointer ( probably Shape ). In theory that should be do-able. One of the problems is that there should be as many Person objects as there are Shape derived types (one for Square, one for Circle, one for Triangle). How do I know how many Person objects to make?