Unique class type Id that is safe and holds across library boundaries

前端 未结 11 670
离开以前
离开以前 2020-12-06 05:52

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 uniq

11条回答
  •  执念已碎
    2020-12-06 06:28

    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.

提交回复
热议问题