使用结构的所有实例调用函数

时间:2013-05-04 13:44:14

标签: c++ function struct

我想调用一个包含所有struct对象的函数。

我需要一个可以通过只调用struct STRUCT_A来遍历struct-objects A_1,A_2的函数。代码底部的空函数'reset_all_structs(???)'。

示例代码:

#include <iostream>


struct STRUCT_A {
    unsigned char number = 0;
    bool bool_1 = 0;
    bool bool_2 = 0;
} A_1, A_2; // Objects: maybe later A_3, ... , A_x

void print_to_terminal(STRUCT_A &STRUCT_NAME);
void set_bool_1(STRUCT_A &STRUCT_NAME);
void set_bool_2(STRUCT_A &STRUCT_NAME);
void reset_one_struct(STRUCT_A &STRUCT_NAME);
void reset_all_structs();

int main()
{

    set_bool_1(A_1);
    A_1.number = 111;
    set_bool_2(A_2);
    A_2.number = 222;
    std::cout << "A_1:\n";
    print_to_terminal(A_1);
    std::cout << "\n";
    std::cout << "A_2:\n";
    print_to_terminal(A_2);
    std::cout << "\n";

    reset_one_struct(A_1); // <-- Reset one struct works, my question ist how to reset all structs with the type STRUCT_A?
    std::cout << "A_1:\n";
    print_to_terminal(A_1);
    std::cout << "\n";

    set_bool_2(A_1);
    A_1.number = 234;
    std::cout << "A_1:\n";
    print_to_terminal(A_1);
    std::cout << "\n";

    // Here the question. ???

    // reset_all_structs( STRUCT_A );

    // I want to reset both A_1 and A_2 by calling the function reset_all_structs with all object of the struct "STRUCT_A" and loop through these. Is this possible
    // I don't want to call a function like reset_all_struct(A_1, A_2) because later I will add more objects of struct STRUCT_A.

    std::cout << "Reset A_1 and A_2\n";
    std::cout << "A_1:\n";
    print_to_terminal(A_1);
    std::cout << "\n";
    std::cout << "A_2:\n";
    print_to_terminal(A_2);
    std::cout << "\n";


    return 0;
}

void print_to_terminal(STRUCT_A &STRUCT_NAME){
    std::cout << "Number: " << (int)STRUCT_NAME.number << "\n";
    std::cout << "bool_1: " << (int)STRUCT_NAME.bool_1 << "\n";
    std::cout << "bool_2: " << (int)STRUCT_NAME.bool_2 << "\n";
    return;
};

void set_bool_1(STRUCT_A &STRUCT_NAME){
    STRUCT_NAME.bool_1 = 1;
    STRUCT_NAME.bool_2 = 0;
    return;
};

void set_bool_2(STRUCT_A &STRUCT_NAME){
    STRUCT_NAME.bool_1 = 0;
    STRUCT_NAME.bool_2 = 1;
    return;
};

void reset_one_struct(STRUCT_A &STRUCT_NAME){
    STRUCT_NAME.number = 0;
    STRUCT_NAME.bool_1 = 0;
    STRUCT_NAME.bool_2 = 0;
    return;
};


void reset_all_structs( ??? ){
// loop through all structs
    return;
};

3 个答案:

答案 0 :(得分:0)

您可以将它们放在本地数组中并循环遍历它,如下所示:

void reset_all_structs()
{
    // This array can be created locally, and must capture by pointer 
    // arrays of references are illegal in C++.
    STRUCT_A * structs[] = { &A_1, &A_2 };
    for(int i = 0; i < sizeof(structs); i++)
    {
      reset_one_struct(*structs[i]);
    }
}

顺便提一下,你标记了你的问题C ++,但这看起来像C.如果你是C ++的新手,我建议你把所有你的STRUCT_NAME带到methods的结构中。至少,将正式的STRUCT_NAME参数重命名为看起来不会与结构名称本身或宏定义发生冲突的内容。最后,我建议将结构实例收集到STL容器中,如std::vector,以便在循环中进行迭代和访问更简单。

答案 1 :(得分:0)

使用array或更好vector的{​​{1}}代替。

structs

答案 2 :(得分:0)

语言本身无法迭代相同类型的相同变量。

您可以对结构进行某种注册,然后遍历此注册表。类似的东西:

struct STRUCT_A;
std::vector<STRUCT_A*> struct_A_registry;

struct STRUCT_A {
   ...
   // constructor
   STRUCT_A() { struct_A_registry.push_back(this); }
   // destructor
   ~STRUCT_A() { 
      for (std::vector<STRUCT_A*>::iterator it = struct_A_registry.begin(); 
           it != struct_A_registry.end(); ++it) 
        if (*it == this) {
          struct_A_registry.erase(this);
          break;
        }
    }
};

然后你的reset_all_structs将只是

void reset_all_structs() {
  for (std::vector<STRUCT_A*>::iterator it = struct_A_registry.begin(); 
       it != struct_A_registry.end(); ++it) 
    reset_one_struct(**it);
}

或者,如果要在程序的同一行声明所有STRUCT_A变量,可以将它们组织成一个数组,这样会简单得多。