Passing variable type as function parameter

后端 未结 4 2030
孤独总比滥情好
孤独总比滥情好 2020-12-24 12:03

Is it possible to pass variable type as part of a function parameter, e.g.:

void foo(varType type)
{
  // Cast to global static
  unsigned char bar;
  bar =          


        
4条回答
  •  不思量自难忘°
    2020-12-24 12:29

    You could make an enum for all different types possible, and use a switch to make the dereferencing:

    typedef enum {
        CHAR,
        INT,
        FLOAT,
        DOUBLE
    } TYPE;
    
    void foo(TYPE t, void* x){
        switch(t){
            case CHAR:
                (char*)x;
                break;
            case INT:
                (int*)x;
                break;
             ...
        }
    }
    

提交回复
热议问题