我有一些以嵌套结构组织的数据,正在尝试访问它。有4层,然后最终结构始终具有2个变量; val
和addr
:
struct Flash{
struct Settings{
struct UnderVoltage{
struct Threshold{
int16_t val = 2850;
uint16_t addr = 0x4481;
} Threshold;
struct Delay{
uint8_t val = 2;
uint16_t addr = 0x4483;
} Delay;
struct Recovery{
int16_t val = 2900;
uint16_t addr = 0x4484;
} Recovery;
} UnderVoltage;
} Settings;
} Flash;
我希望能够具有以下功能
void writeThreshold(){
writeFlash( Flash.Settings.UnderVoltage.Threshold );
}
然后调用像
void writeFlash( struct dataParam ){
byte addr1 = dataParam.addr & 0xFF;
byte addr2 = ( dataParam.addr >> 8 ) & 0xFF;
uint8_t byteLen = sizeof( dataParam.val );
byte valBytes[ byteLen ];
for( int i = 0; i < byteLen; i++ ){
valBytes[ i ] = dataParam.val >> ( i * 8 );
}
}
那么有没有一种方法可以将结构嵌套发送给函数,以便函数可以访问其中的变量?
谢谢
最佳答案
您可以使用功能模板:
template<typename T>
void writeFlash(const T& dataParam){
// ... as before
关于c++ - C++将结构传递给函数以访问嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59649714/