继承静态变量成员,但单独共享给各类继承类

发布于 2024-12-19 08:36:20 字数 404 浏览 1 评论 0原文

如果类继承具有静态变量成员的基类,则将是它们唯一一个与继承的所有类共享的成员。

我有几种继承类,并且每个类都有很多实例。 我希望每个继承类都有一个单独的静态成员,并与其所有实例共享。

怎么办呢?

谢谢你,对我糟糕的英语感到抱歉。

编辑:

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

现在,我希望 b 的所有实例都具有相同的 var ,并且 c 的所有实例都具有相同的 var ,但是 var b 的 var 与 c 的 var 不同。

我再次对我的英语感到抱歉,如果你能纠正我,请纠正我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2024-12-26 08:36:21

您可以使用 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.
};

AClassAnotherClass 都有一个 variable 静态变量(类型为 Member) ,但第一个是来自 YourBaseClass 的静态变量,另一个是来自 YourBaseClass 的静态变量,这是两个不同的类。

YourBaseBaseClass 是可选的,但如果您想使用 YourBaseBaseClass* 指针操作 AClassAnotherClass,则需要它(你不能有 YourBaseClass* 指针,因为 YourBaseClass 不是一个类)。

并记住定义那些静态变量。

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.

时光无声 2024-12-26 08:36:21

在这种情况下,您不能将静态成员放入基类中。但您可以尝试将其放在派生类中,并通过调用基类中的静态方法来访问它。因此 Derived 和 Derived2 的实例可以共享不同的静态成员。如果派生类的 GetStaticValue() 方法被调用,则强制派生类定义一个名为 value 的静态成员。

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();
}

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();
}
暖心男生 2024-12-26 08:36:21

静态成员与非静态成员不同。静态成员称为类变量,非静态成员称为实例变量。这是因为非静态成员属于特定对象(类的实例),而静态变量是共享的。

因此,静态成员不遵循相同的继承规则,并且仍然是定义它们的类的属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文