[英]How to expose an array of functions as a class member in C++?
I have to expose an array of functions in my application. 我必须在应用程序中公开一系列函数。 These functions are actually methods of that class and I will populate the array in the constructor.
这些函数实际上是该类的方法,我将在构造函数中填充数组。 For instance:
例如:
void Cpu::print() { // some func
std:cout << "hi";
}
void Cpu::Cpu() { // class ctor
funcArray = { &Cpu::print }
}
Then I want to do this: 然后我要这样做:
Cpu myCpu;
(myCpu.*funcArray[0])();
All my functions will follow the same signature "void ()". 我所有的函数都将遵循相同的签名“ void()”。
Is that possible? 那可能吗?
You cannot assign to arrays, but you can use initialiser lists: 您不能分配给数组,但可以使用初始化程序列表:
class Cpu {
public:
typedef void (Cpu::*Func)();
...
Func funcArray[CONSTANT];
};
Cpu::Cpu() : funcArray({ &Cpu::print }) {
// the rest of the array is filled with NULL pointers
}
Then 然后
Cpu cpu;
(cpu.*(cpu.funcArray[0]))();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.