yoni
yoni

Reputation: 1364

Inheritance static variable member, but share it separately to every kind of the inheritance class

If class inherits base class with static variable member, Will be their only one member that shared with all classes that inheritances.

I have few kinds inherits classes, and many instance of every one of them. I want that every one of the inherits classes will have a separate static member, that shared with all of its instances.

How can it be done?

thank you, and sorry about my poor English.

edit:

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

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

I'm sorry again about my English, if you can correct me, please do it.

Upvotes: 4

Views: 1506

Answers (3)

Shawnone
Shawnone

Reputation: 860

You cannot put the static member in the base class in this scenario. But you can try to put it in the derived class and access it by calling an static method in the base class. So instances of Derived and Derived2 can share different static members. And you force the derived class to define a static member named 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();
}

Upvotes: 2

BatchyX
BatchyX

Reputation: 5114

You can work aroud that 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 variable static variable (of type Member), but the first one is a static variable from YourBaseClass<AClass> and the other is from YourBaseClass<AnotherClass>, which are two different classes.

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

And remember to define those static variables.

Upvotes: 10

Gigi
Gigi

Reputation: 29421

Static members are different from non-static members. Static members are called class variables, while non-static members are called instance variables. This is because non-static members belong to a particular object (instance of a class), while static variables are shared.

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

Upvotes: 0

Related Questions