一部の定数属性が異なる派生クラスがあります。すべての派生クラスで、属性を返す関数があります。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 が定義されていないため、これは機能しません。また、extern、静的 const 属性 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;
}
};
ありがとう。