我派生的类在某些常量属性上有所不同。在所有派生类中,我都有一个返回属性的函数。有没有办法将 get_x 函数移到基类中以删除重复项?我查看了这个线程和很多谷歌搜索,但我找不到我想要的东西: C++:在派生类中用不同的值初始化基类常量静态变量?
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;
}
};
我希望它看起来像这样,但这不起作用,因为 x 没有在 base.xml 中定义。我也尝试过 extern、static const attribute x 等。
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;
}
};
谢谢。