Inherit a static variable member, but divide it separately for each type of inheritance class

If a class inherits a base class with a static variable member, it will be their only member that shares with all inheritance classes.

I have several types inheriting classes, and many instances of each of them. I want each of the inheritance classes to have a separate static member that shares with all its instances.

How can I do that?

thank you and sorry for my bad english.

edit:

class a{ static int var;}; class b::a{}; class c::a{}; 

Now I want all instances of b to have the same var, and all instances of c to have the same var, but var b will be different from var c.

I regret my English, if you can correct me, please do so.

+4
source share
3 answers

You can work using CRTP:

 struct YourBaseBaseClass { // put everything that does not depend on your static variable }; template <YourSubclass> struct YourBaseClass : YourBaseBaseClass { static Member variable; // and everything that depend on that static variable. }; struct AClass : YourBaseClass<AClass> { // there is a "variable" static variable here }; struct AnotherClass : YourBaseClass<AnotherClass> { // which is different from the "variable" static variable here. }; 

AClass and AnotherClass both have a static variable variable (of type Member ), but the first is a static variable from YourBaseClass<AClass> , and the other from YourBaseClass<AnotherClass> , which are two different classes.

YourBaseBaseClass is optional, but you need it if you want to manipulate AClass and AnotherClass using the YourBaseBaseClass* pointer (you cannot have a YourBaseClass* pointer because YourBaseClass not a class).

And don't forget to define these static variables.

+10
source

You cannot put a static member in a base class in this scenario. But you can try to put it in a derived class and access it by calling the static method in the base class. Thus, instances of Derived and Derived2 can share different static elements. And you force the derived class to define a static member called value if its GetStaticValue () method is called.

 template <typename T> class Base { public: static int GetStaticValue() { return T::value; } }; class Derived : public Base<Derived> { friend class Base<Derived>; private: static int value; }; int Derived::value = 1; class Derived2 : public Base<Derived2> { friend class Base<Derived2>; private: static int value; }; int Derived2::value = 2; int main() { Derived d; Derived da; int ret = d.GetStaticValue(); ret = da.GetStaticValue(); // As everything is static you don't need to instantiate the Derived classes ret = Base<Derived>::GetStaticValue(); Derived2 d2; Derived2 d2a; ret = d2.GetStaticValue(); ret = d2a.GetStaticValue(); } 
+2
source

Static elements differ from non-static elements. Static members are called class variables, and non-static members are called instance variables. This is due to the fact that non-static members belong to a specific object (class instance), and static variables are shared.

Thus, static members do not follow the same rules of inheritance and remain a property of the class that defines them.

0
source

Source: https://habr.com/ru/post/1384575/


All Articles