I am trying to provide different static initializations for classes in a hierarchy, but when I tried with this code:
#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; }
I ended up with this build error:
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
It seems that static initializations cannot be overridden on derived classes? If this does not work, I can always determine that combine_name is a static function returning const char *, the only problem is that I kind of hoped to do initializations for partial specialization, and there seems to be no one that I know to override only one function in partial specialization without copying all other code that will remain basically the same
source share