階層内のクラスにさまざまな静的初期化を提供しようとしていますが、このコードで試した場合:
#include <iostream>
using namespace std;
struct base {
static const char* componentName;
};
const char* base::componentName = "base";
struct derived : public base {};
const char* derived::componentName = "derived";
int main() {
cout << base::componentName << endl;
cout << derived::componentName << endl;
}
私はこのビルドエラーで終わりました:
test.cpp:15: error: ISO C++ does not permit ‘base::componentName’ to be defined as ‘derived::componentName’
test.cpp:15: error: redefinition of ‘const char* base::componentName’
test.cpp:11: error: ‘const char* base::componentName’ previously defined here
静的初期化は派生クラスでオーバーライドできないようですか?これが機能しない場合は、常にcomponentNameをconst char *を返す静的関数として定義する可能性があります。これに関する唯一の問題は、部分的な特殊化の初期化を行うことを望んでいたことであり、その方法はないようです。ほとんど同じままになる他のすべてのコードをコピーせずに、部分的な特殊化で1つの関数だけを再定義することを知っています。