在扩展模板的类中初始化静态常量

发布于 2024-10-27 00:29:51 字数 1030 浏览 2 评论 0原文

考虑以下伪代码:

class Foo {
public:
    virtual int getID() const = 0;
}

template<typename T>
class Blah : public Foo {
public:
    T data;
    static const int ID;  //static ID
    int getID() const { return Blah<T>::ID; }  //instance returns the ID
}

class Dude : public Blah<int> {
}
int Dude::ID = 10;  //I want to define Blah<int>::ID here, but how?

int receive(const Foo& foo) {
    if(foo.getID() == Dude::ID) {
        cout << "Received a Dude" << endl;
    }
}

这段代码无法编译,因为 ISO C++ 不允许将 Blah 模板中的 ID 定义为 Dude 类中的 ID。我明白为什么,因为我可以有多个扩展 Blah 的类。

我明白如果我把 template; Blah::ID = 10' int Blah 暗示它将起作用...但这不是我想要的...我希望派生类定义 ID。 ..

我是否必须将 ID 和 getID() 推送到派生类中?我想最终我对一些 RTTI 感兴趣,这样我就可以适当地处理 Foo 。如果有人有更好的模式,我洗耳恭听。

编辑 为了回应一些评论...我想通过某个 ID 唯一地标识从 Foo 派生的类,以便我可以将某些 Foo 对象的运行时 id 与特定的类 ID。

谢谢!

Consider this pseudocode:

class Foo {
public:
    virtual int getID() const = 0;
}

template<typename T>
class Blah : public Foo {
public:
    T data;
    static const int ID;  //static ID
    int getID() const { return Blah<T>::ID; }  //instance returns the ID
}

class Dude : public Blah<int> {
}
int Dude::ID = 10;  //I want to define Blah<int>::ID here, but how?

int receive(const Foo& foo) {
    if(foo.getID() == Dude::ID) {
        cout << "Received a Dude" << endl;
    }
}

This piece of code fails to compile because ISO C++ does not permit the ID in the Blah template to be defined as the ID in the Dude class. I understand why because I could have multiple classes that extend a Blah<int>.

I understand if I put template<typename T> int Blah<T>::ID = 10' in the Blah<T> impl that it will work...but that isn't what I want...I want the derived class to define the ID...

Do I have to push the ID and getID() into the derived class? I guess ultimately I'm interested in some RTTI so I can process the Foo appropriately. If anyone has a better pattern, I'm all ears.

EDIT
In response to some of the comments...I would like to uniquely identify classes that derive from Foo via some ID so I can compare the runtime id of some Foo object to a specific class id.

Thanks!

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

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

发布评论

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

评论(3

万劫不复 2024-11-03 00:29:51

制作静态int ID; private,并在公共接口中提供GetID,使SetID成为受保护的接口。但这不是一个好的解决方案,因为所有派生类将共享相同的 ID,这不是您想要的。

更好的方法应该是使用 id 作为基类的模板参数,然后 class Derived : public Base<234>{} 将起作用。

或者将 virtual const int GetID() = 0 添加到 Base 类中。

Make the static int ID; private, and provide GetID in public interface, make SetID a protected interface. But that is not a good solution, because all the derived class will share the same ID, which is not what you want.

A better way should be you use the id as the base class' template parameter, then class Derived : public Base<234>{} will work.

Or add virtual const int GetID() = 0 into Base class.

风蛊 2024-11-03 00:29:51

我认为你可以简单地这样做:

class Dude : public Blah<int> {
}
 static const int Dude_ID; //declaration!

int receive(const Foo& foo) {
    if(foo.getID() == Dude::Dude_ID) {
        cout << "Received a Dude" << endl;
    }
}
static const int Dude::Dude_ID = 10; // definition!

同样,为每个派生类定义一个ID。


为每个类提供 ID 的另一种方法是:

template<typename T, int ID=1>
class Blah : public Foo {
public:
    int getID() const { return ID; }  
}

template<int ID=10>
class Dude : public Blah<int> {
public:
    int getID() const { return ID; }  
}

I think you can simply do this:

class Dude : public Blah<int> {
}
 static const int Dude_ID; //declaration!

int receive(const Foo& foo) {
    if(foo.getID() == Dude::Dude_ID) {
        cout << "Received a Dude" << endl;
    }
}
static const int Dude::Dude_ID = 10; // definition!

Similarly, define an ID for each derived class.


Yet another way to have an ID for each class is this:

template<typename T, int ID=1>
class Blah : public Foo {
public:
    int getID() const { return ID; }  
}

template<int ID=10>
class Dude : public Blah<int> {
public:
    int getID() const { return ID; }  
}
上课铃就是安魂曲 2024-11-03 00:29:51

我发现这个答案正是我所追求的……很抱歉,如果我的问题令人困惑。

在C++中,如何使用单例来保证每个类都有唯一的整体ID?

I found this answer that does exactly what I'm after...sorry if my question was confusing.

in C++, how to use a singleton to ensure that each class has a unique integral ID?

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