传递变量类型为函数参数 [英] Passing variable type as function parameter

查看:149
本文介绍了传递变量类型为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过变量类型作为函数参数,例如部分:

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 = ((type *)(&static_array))->member;
}

我记得它是与海湾合作委员会的的typeof 和使用宏?

推荐答案

您不能这样做,对于一个功能,因为那时它需要知道的参数类型(和函数使用任何其他符号)来生成工作机code。你可以尝试像宏:

You can't do that for a function, because then it needs to know the types of the arguments (and any other symbols the function uses) to generate working machine code. You could try a macro like:

#define foo(type_t) ({ \
    unsigned char bar; \
    bar = ((type_t*)(&static_array))->member; \
    ... \
    })

这篇关于传递变量类型为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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