是否可以在C ++类中声明一个虚拟静态常量值? [英] Is it possible to declare a virtual static constant value in a C++ class?

查看:125
本文介绍了是否可以在C ++类中声明一个虚拟静态常量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个基类,它有一个常量字段(像一个唯一的ID,与编译时无法修改的类相关联)。到目前为止, static const 声明就可以了。现在,我想继承这个基类,并确保这个类的孩子有相同的字段,但有自己的值。如何做到这一点?



让我们想要一个基类 Base ID 字段,其中包含 int 值0.然后,我想有 A B C c $ c> Base ,并且我想确保这些孩子也有 ID 字段,各自的值为1,2和3(通过'确定',我的意思是,如果他们没有一个编译器错误,如果没有明确声明ID)。



如果我可以管理构建这个场景,我的期望是要求 Base * 指针的 ID 字段,我应该得到不同的值取决于指针是否创建为 new A() new B() new C()



我的猜测是将 ID 声明为 virtual static const ,这当然没有意义,并给出一个编译器错误。



但是我可以做什么实现描述的结果? (我可以想象的唯一的事情是声明 ID 作为一个虚函数返回一个整数,然后硬编码的值到函数体,但我看



提前感谢!

解决方案

A static 方法不能是 virtual ,并且没有数据成员可以 virtual static 字段,并使用

> virtual 方法返回它们。

  class A 
{
public :
static const int ID = 0;
virtual int getID(){return A :: ID; }
};
class B:A
{
public:
static const int ID = 1;
virtual int getID(){return B :: ID; }
};

替代方法:

 code> class A 
{
public:
A(int id = 0):ID(id){}
const int ID;
getID(){return ID; }
};
class B:public A
{
public:
B():A(1){}
};


I'd like to have a base class that has a constant field (like an unique ID associated with the class that can't be modified after compile time). So far the static const declaration would be just fine. Now, I'd like to inherit this base class and make sure that the children of this class do have the same field, but with their own values. How can I do this?

Let's say, I'd like to have a base class called Base with an ID field that holds the int value of 0. Then, I'd like to have the classes A, B and C, all of them being public children of Base and I'd like to make sure that these children would also have the ID fields with the respective values of 1, 2 and 3 (by 'making sure', I mean something like getting a compiler error if they don't have a ID explicitly declared).

If I could manage to build this scenario, my expectation would be that asking for the ID field of a Base* pointer, I should get different values depending whether the pointer was created as new A(), new B() or new C().

My guess would be to declare ID as virtual static const, which of course doesn't make sense and gives a compiler error.

But then what can I do to achieve the described result? (The only thing that I could imagine would be to declare ID as a virtual function returning an integer and then hard-code the value into the function body, but I'm looking for something more elegant.)

Thank you in advance!

解决方案

A static method cannot be virtual, and no data members can be virtual.

But you can hide static fields in derived classes and use a virtual method to return them.

class A
{
public:
    static const int ID = 0;
    virtual int getID() { return A::ID; }
};
class B : A
{
public:
    static const int ID = 1;
    virtual int getID() { return B::ID; }
};

Alternative:

class A
{
public:
    A(int id = 0) : ID(id) {}
    const int ID;
    getID() { return ID; }
};
class B : public A
{
public:
    B() : A(1) {}
};

这篇关于是否可以在C ++类中声明一个虚拟静态常量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆