I have a hierarchy of classes with a static variable:
class Shape{
public:
static string name;
}
class Rectangle: public Shape{
}
I would like to set name
according to the class. So Shape::name
should be "Shape" and Rectangle::name
should be "Rectangle"
What I did is initialize the static variable in each of the .cpp implementation files. So in Shape.cpp:
string Shape::name = "Shape";
And in Rectangle.cpp:
string Shape::name = "Rectangle";
The linker does not like that and complains that there is a duplicate symbol. So how can I achieve that?
Note: I would like to stick with constructors with initialization lists (no implementation in the .cpp)