我是 C++ 新手,我想知道我们是否可以接受任何结构作为我们的方法参数。
情况就是这样:我有一个类(比方说hw_manager
)与硬件类(比方说)交互hw_device1
。目前hw_manager
会调用 的方法,方法hw_device1
的结果会通过 struct 参数返回(将 struct 参数作为引用发送,并更改引用参数的值)。
在 C++ 中代码应该是这样的:
struct sSensorStatus {
unsigned char sensor1;
unsigned char sensor2;
};
bool checkSensorStatus(struct sSensorStatus &status) {
// Change the status here
}
现在,由于硬件发生了变化,我需要创建一个新类,假设hw_device2
它具有完全不同的操作。
struct sHardwareStatus {
unsigned char loader;
unsigned char transport;
unsigned char ejector;
unsigned char mouth;
};
bool checkHardwareStatus(struct sHardwareStatus &status) {
// Change the status here
}
hw_manager
我打算实现一个接口,而不是更改其中的代码(这将影响该层之上的代码),假设IHardware
它具有doAction
方法。这个想法是这样的:
bool doAction(int cmdID, ????) {
// switch case cmdID
// based on the cmdID, type cast the ???? into the struct
}
我应该放什么????接受任何类型的结构?我可以在 C++ 中做到这一点吗?
谢谢
编辑
在硬件内部,我还会有另一个结构,所以我认为使用模板不合适。抱歉,澄清晚了。