Cannot override static initialization in derived class

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

+4
source share
6 answers

You also need to declare it in your subclass.

 struct derived : public base { static const char* componentName; }; 
+6
source

A static member variable means that there is one variable that is used for all instances of this class. Trying to have one value for the base class and another value for the derived class does not work, because they both use the same variable, which (obviously enough) cannot be set to two different values โ€‹โ€‹at the same time.

+2
source


I think the reason is that the following is true:

&base::componentName == &derived::componentName

they refer to the same object and initialize the object twice in
"who laughs last, laughs, the best" manner may not be good.

Greetings.

Vintz

+2
source

'overridding' and 'inheritance' are terms that only make sense with objects. Class variables do not participate in object inheritance.

0
source

$ 9.4.2 / 2 - "In the definition in the namespace area, the name of the static data member must be qualified by its name using the :: operator."

A...

It seems that static initializations cannot be overridden by classes?

Remember that overriding occurs only for virtual functions.

$ 10.3 / 2 - "If the virtual member of the vf function is declared in the Base class and in the Derivatives class, derivatives directly or indirectly from the base, the vf function member with the same name and the same parameter list as Base :: vf. Is declared, then Derived :: vf is also virtual (regardless of whether it is so declared) and it cancels 97) Base :: .

You can try to find the name of the component so as to take advantage of working with polymorphic code.

 struct base{ virtual char const* myname(){ return "base"; } virtual ~base(){} }; struct derived : base{ virtual char const* myname(){ return "derived"; } }; int main(){} 
0
source

if you try to initialize a static variable in a derived class before declaring in the derived class, you will get an override error because the base class based on the derived class and static variables are defined only once for the class, so the second initialization causes an override error.

One of the right ways to do what you intend is below;

 struct a { virtual const string& getClassType() const { return ClassName; } static string ClassName; }; string a::ClassName = "StructA"; struct c : public a { const string& getClassType() const { return ClassName; } static string ClassName; }; string c::ClassName = "StructC"; a* a1 = new c(); cout << a1->getClassType() << endl; 

Note In the above code, getClassType is a virtual function and returns the class type in string format. This function uses a static variable and must also be overridden in a derived class. If you forget to override it, the compiler will call the version of the base class function and use the static variable of the base class instead of the static variable of the derived class. Thus, it will return the object type of the base class.

0
source

All Articles