C++ unique static id and class name with base class

人盡茶涼 提交于 2019-12-20 03:07:05

问题


Having class TaskBase, each derived class of it must have name and unique id.

The TaskBase is something like below:

class TaskBase
{
public:
    static const int id()
    {
        // return an unique id, for each object or derived class, HOW ??
    }

    static const string name()
    {
        // return class name for each derived class, HOW ??
        // for example : "TaskBase" for this class
    }
};

My try was :

template <typename DERIVED>
class TaskBase
{
public:

    static const int id() 
    {
        static const int id = reinterpret_cast<int> (typeid (DERIVED).name());
        return id;
    }

    static const string name() 
    {
        static string n;

        if (!n.size())
        {
            int status;
            char *realname = abi::__cxa_demangle(typeid (DERIVED).name(), 0, 0, &status);
            n = realname;
            free(realname);
        }

        return n;
    }
};

I already read this, but i need the ability to have base pointer to each derived classes, something line below:

class MyTask1 : public TaskBase
{
};

MyTask1 myTask1, myTask2;
TaskBase *base = &myTask1;

回答1:


class TaskBase
{
private:
    const void*  m_id;
    string m_name;

public:
    TaskBase(const void* m_id, string m_name): m_id(m_id), m_name(m_name)
    {
    }

    const void* id() const
    {
        return m_id;
    }

    string name() const
    {
         return m_name;
    };
};

template< typename DERIVED >
class TaskProxy: public TaskBase
{
public:   
    static const void* id()
    {
        //if you want to have for each object a unique id:
        //return reinterpret_cast<void*>(this);
        //just for each TaskProxy<????>:
        return reinterpret_cast<const void*>(typeid( DERIVED ).name());
    }

    static string name()
    {
        return typeid( DERIVED ).name();
    }

    TaskProxy(): TaskBase(id(), name()) {}
};

Usage:

class MyTask1 : public TaskProxy< MyTask1 >
{
};

class MyTask2 : public TaskProxy< MyTask2 >
{
};

...

MyTask1 myTask1;
TaskBase *baseA = &myTask1;
MyTask2 myTask2;
TaskBase *baseB = &myTask2;

cout << "Name: " << baseA->name() << "  Id:" << baseA->id() << endl;
cout << "Name: " << baseB->name() << "  Id:" << baseB->id() << endl;

Which outputs this (with gcc 4.6):

Name: 7MyTask1  Id:0x401228
Name: 7MyTask2  Id:0x4011c0



回答2:


I suggest implementing pure virtual methods for obtaining the class name and ID in the base class. The descendants would need to provide the unique names and IDs.

class TaskBase
{
    public:
        virtual std::string  get_task_name(void) const = 0;
        virtual unsigned long get_task_id(void) const = 0;
};

I took @VoidStar's suggest a step further and put the names into a (single) common class:

class TaskNames
{
  protected:
    static std::string get_tas1_name();
};

class Task1: public TaskBase, public TaskNames
{
//...
};



回答3:


If you are following strictly standard C++, you may need to just bite the bullet and do some additional bookkeeping. Make an enum somewhere that stores all the classnames:

enum ClassID {
    MYTASK1_CLASS,
    MYTASK2_CLASS
};

It doesn't take that long to add a new classId when you make a new class.

I've done this before. It's sufficient for uniqueness to do what I describe above. But... if the enum values are set with a clever enough macro, you can encode the hierarchy of the classes, and implement dynamic cast and instanceof solely from the ClassID and a bitwise mask!



来源:https://stackoverflow.com/questions/7691697/c-unique-static-id-and-class-name-with-base-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!