C ++:使用变量名作为字符串参数设置函数

时间:2019-12-23 11:26:39

标签: string variables search setter getter

我遵循的是 IEEE 802.15.4 标准,该标准要求设置器功能set(attribute, attributeValue),其中

  • attributechar[]
  • attributeValueunsigned char[]unsigned charunsigned int之间是可变的,

我必须使用char数组搜索变量名称,然后才能进行设置并获取它。相信必须使用某些功能,例如map()。像这样建立起来,并对属性变量进行某种不同的处理。

main()实际上不会在同一文件中,但是为了简单起见,在这里。

值得一提的是,与 C ++ 相比,我对 C 更有经验。

#include <stdio.h>

typedef unsigned char byte;

class ClassA {
private:
    // Attributes, probably have to be stored in another way
    byte attributeA;
    byte attributeB[2];
    unsigned int attributeC;
    byte attributeD[];
    unsigned int attributeE;
public:
    // Setters
    void set(char attribute[], byte attributeValue) {
        // Search and set attribute
    }

    void set(char attribute[], byte attributeValue[]) {
        // Search and set attribute
    }

    void set(char attribute[], unsigned int attributeValue) {
        // Search and set attribute
    }

    void print() {
        printf("Value of attributeC: %u", attributeC);
    }
};

int main() {
    char attributeToSet[] = "attributeC";
    unsigned intattributeValueToSet = 5;

    ClassA::set(attributeToSet, attributeValueToSet)
    ClassA::print();
    return 0;
}

0 个答案:

没有答案