I have derived classes that differ in some constant attribute. In all derived classes, I have a function that returns attribute. Is there a way to move the get_x function into the base class to remove the duplication? I have looked in this thread and a lot of google searches but I couldn't find exactly what I want: C++ : Initializing base class constant static variable with different value in derived class?
class Derived1: public Base{
static const attribute x = SOME_ATTRIBUTE1;
attribute get_x(){
return x;
}
};
class Derived2: public Base{
static const attribute x = SOME_ATTRIBUTE2;
attribute get_x(){
return x;
}
};
I would hope that it would look something like this, but this doesn't work because x isn't defined in base. I've also tried extern, static const attribute x, etc.
class Derived1: public Base{
static const attribute x = SOME_ATTRIBUTE1;
};
class Derived2: public Base{
static const attribute x = SOME_ATTRIBUTE2;
};
class Base{
attribute get_x(){
return x;
}
};
Thanks.