首页 > 解决方案 > 将通用 typdef 结构传递给 C++ 中的函数

问题描述

我正在研究 Arduino,我正在编写通用 cpp 类以在 i2c 中进行通信。我想知道是否可以typedef struct在函数中传递一个表示寄存器的泛型,像这样

typedef struct {
  uint8_t not_used_01    : 1;
  uint8_t i3c_disable    : 1;
  uint8_t den_lh         : 2;
  //some other attributs
} reg_ctrl9_xl_t;

typedef struct {
  uint8_t not_used_01    : 1;
  uint8_t odr_h          : 1;
  uint8_t ord_lh         : 1;
  //some other attributs
} reg_ctrl5_g_t;

我的头文件

class I2c
{
   public:
      I2c(uint8_t address);
      void writeStruct(uint8_t register_addr, <what data type here??> register_struct); 
      //some other functions   
   private:
     uint8_t _address;   
}

我不知道这是否可能,我不知道我应该输入哪种数据类型,register_struct因为这里我有 2 个寄存器,例如reg_ctrl9_xl_t,我reg_ctrl5_g_t正在寻找一个通用参数函数writeStruct

标签: c++arduino

解决方案


推荐阅读