在扩展模板的类中初始化静态常量
考虑以下伪代码:
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
暗示它将起作用...但这不是我想要的...我希望派生类定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
制作静态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.
我认为你可以简单地这样做:
同样,为每个派生类定义一个ID。
为每个类提供 ID 的另一种方法是:
I think you can simply do this:
Similarly, define an ID for each derived class.
Yet another way to have an ID for each class is this:
我发现这个答案正是我所追求的……很抱歉,如果我的问题令人困惑。
在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?