我如何才能以特定方式使用数组中的函数。现在,我把它设置成这样:
typedef void (*func_ptr)(void);
const func_ptr functions[] = {a, b};
inline void a(void)
{
something = blah;
}
inline void b(void)
{
anotherthing = blahblah;
}
我在想是否有办法缩短一点,也许是这样的:
const func_ptr functions[] = {(void)(something = blah;), (void)(anotherthing = blahblah;)};
内联函数 a 和 b 只包含一行代码,设置一些 #define
。
最佳答案
在 C 中你不能有匿名函数。
不过在 C++11 中,非捕获 lambda 会衰减为普通函数指针,因此您可以:
typedef void (*func_ptr)(void);
const func_ptr functions[] = {
[]() { something = blah; },
[]() { anotherthing = blahblah; }
};
关于c - 功能类似于数组中的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26020665/