唯一的类类型ID,它是安全的并且跨库的边界 [英] Unique class type Id that is safe and holds across library boundaries

查看:161
本文介绍了唯一的类类型ID,它是安全的并且跨库的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会感谢任何帮助,因为C ++不是我的主要语言。



我有一个模板类,在多个库中派生。我试图找出一种方法来唯一地分配一个id int到每个派生类。我需要能够通过静态方法,即



  
template< class DERIVED>
class Foo
{
public:
static int s_id()
{
// DERIVED的返回ID是唯一的
}
// ...
};


谢谢!

解决方案

这是我最后做的。如果您有任何意见(利弊),请通知我们。



  
template< class DERIVED>
class Foo
{
public:
static const char * name(); //派生类将实现,简单地

//返回它们的类名
static int s_id()
{
static const int id = Id_factory :: get_instance ) - > get_id(name());
return id;
}
// ...
};



基本上,在进行字符串比较而不是指针比较之后,这在速度上是不理想的,但我做了id静态const,所以它只需要为每个DERIVED计算一次。


I would appreciate any help as C++ is not my primary language.

I have a template class that is derived in multiple libraries. I am trying to figure out a way to uniquely assign an id int to each derived class. I need to be able to do it from a static method though, ie.


template < class DERIVED >
class Foo
{
public:
    static int s_id()
    {
        // return id unique for DERIVED
    }
    // ...
};

Thank you!

解决方案

Here's what I ended up doing. If you have any feedback (pros, cons) please let me know.


template < class DERIVED >
class Foo
{
public:
    static const char* name(); // Derived classes will implement, simply
// returning their class name static int s_id() { static const int id = Id_factory::get_instance()->get_id(name()); return id; } // ... };

Essentially the id will be assigned after doing a string comparison rather than a pointer comparison. This is not ideal in terms of speed, but I made the id static const so it will only have to calculate once for each DERIVED.

这篇关于唯一的类类型ID,它是安全的并且跨库的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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