如何知道C++中void*数组的元素类型

how to know the element type of void* array in c++

首先我在编译器项目上工作,我构建了一个符号 table

class SymbolTable
{
    Scope * currScope;
    Scope * rootScope;
...
}

//where scope is 
class Scope{
    Scope();
    Scope * parent;
    MyMap * m;
...
};

//and Mymap is 
class MyMap
{
    static const int mapLength = MAX_LENGTH;
    MapElem * arr[mapLength];
    int hash(char* name);
...
}

//MapElem is

    class MapElem{
        char* name;
        void* elem;
        MapElem * next;
    ...
    }

现在 Void* elem 可以是 ((function , class, variable ,scope)) 所有 r 类, 我想打印符号 table 来检查 Yacc 和解析器在做什么!! 我试着这样做:

void printScope(Scope *s)
{
    if (s != NULL)
    {
        cout << "{";
        for (int i = 0; i < 71; i++)
        {
            MapElem* tempelem = s->m->getbyId(i);
            while (tempelem != NULL)
            {
                //cout << "element name is" << tempelem->getName();

                if (static_cast <Type*> (tempelem->getElem())){
                    Type* t = (Type*)tempelem->getElem();
                    cout << "element is Class it's name is" << t->getIs_final() << " " << t->get_name() << "(";
                    for (int i = 0; i < t->getInheritedType().size(); i++){
                        if (t->getInheritedType()[i] != NULL)
                        cout << t->getInheritedType()[i]->get_name() << "," << endl;
                    }
                    cout << "):" << endl;
                    printScope(t->getScope());
                }

                else if (static_cast <Function*>(tempelem->getElem())){
                    Function* t = (Function*)tempelem->getElem();
                    cout << "element is Function it's name is" << t->get_final() << " " << t->get_name() << "(";
                    vector<Variable *> paramet = t->getparameters();
                    for (int i = 0;i< paramet.size(); i++){
                        cout << paramet[i]->get_name() << "," << endl;
                    }
                    cout << "):" << endl;
                    printScope(t->getScope());
                }
                else if ((Scope*)tempelem->getElem()){
                    Scope* t = (Scope*)tempelem->getElem();
                    printScope(t);
                }
                else if ((Variable*)tempelem->getElem()){
                    Variable* t = (Variable*)tempelem->getElem();
                    cout << "element is Variable it's name is" << t->getAccessModifier() << " " << t->get_name() << endl;
                }
                tempelem = tempelem->getNext();
            }
        }
        cout << "}"<<endl;
    }


}

代码运行完美,但它不检查 If 语句中的 [void type],总是输入第一个条件,即使转换错误, 按照这个顺序总是输入类型,即使 void 是函数或变量??? 当我更换它们时,也输入第一个 stmt 到底是什么!!! 为什么 ??我该如何解决??或者我怎么知道我必须转换什么数据类型。

传统的答案是在MapElem中添加一个枚举来表示类型:

class MapElem{
    //Enumeration identifying all the types of map element and indicating the contents of elem.
    typedef enum {
        aFunction,
        aClass,
        aVariable,
        aScope
    } Type;

    char* name;
    Type type; //<---- Tells us what elem really is!
    void* elem;
    MapElem * next;
...
};

更面向对象的方法是为每个类型引入一个基础class和子class。 您可能会发现这有点麻烦,因为变量类型(函数、class、变量和范围)是如此不同,以至于您的 base-class 仅包含一种提取类型的方法! 哦还有一些方法 returns 一个用于检查元素的可打印字符串...

您可能会引入一个虚拟成员,returns 枚举类型或依赖 RTTI。 RTTI 通常是一个错误,表明您不了解多态性或者它并没有真正帮助这种情况。 在这种情况下,我怀疑是后者。