用结构体的所有实例调用函数
Call function with all instances of a struct
我想用所有的struct对象调用一个函数。
我需要一个函数,它可以通过只调用STRUCT_A来循环遍历结构对象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_2n";
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;
};
语言本身不可能遍历相同类型的相同变量。
您可以为结构体创建某种注册表,然后遍历该注册表。比如:
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变量,您可以将它们组织成一个数组,这会更简单。
你可以把它们放在一个局部数组中,然后遍历它,像这样:
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
)中,以简化循环中的迭代和访问。
使用array
或更好的structs
的vector
代替。
struct STRUCT_A {
unsigned char number = 0;
bool bool_1 = 0;
bool bool_2 = 0;
};
typedef struct STRUCT_A Data;
void set_bool_1(Data& data_i) {
data_i.bool_1 = 1;
}
void set_bool_2(Data& data_i) {
data_i.bool_1 = 1;
}
void reset_one_struct(Data& data_i) {
data_i.bool_1 = 0;
data_i.bool_2 = 0;
data_i.number = 0;
}
void reset_all_structs(std::vector<Data>& all_data) {
for(int i = 0; i < all_data.size(); ++i)
reset_one_struct(all_data[i]);
}
int main()
{
std::vector<Data> my_data(2);
set_bool_1(my_data[1]);
my_data[1].number = 111;
reset_one_struct(my_data[1]);
reset_all_structs(my_data);
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 从基类实例调用派生类方法而不进行强制转换
- 如何在C++从父实例调用子函数
- 为 std::shared_ptr 实例调用 Operator()
- 跨类的多个实例调用函数一次
- 如何从派生类实例调用父类重载函数
- 如何在C++中跨实例调用方法
- 防止同一类的实例调用公共功能(C )
- 使用Objective C类实例调用C函数
- 从结构实例调用变量语法问题C++
- 从基类实例调用派生类方法
- C++从基类实例调用派生函数
- 是否可以从不存在的实例调用方法?
- 如何使用派生类实例调用基类重载的func,
- 为singleton实例调用函数创建一个别名
- 用结构体的所有实例调用函数
- 从继承的类实例调用静态成员
- 当我从类实例调用方法时,我的C程序崩溃了,下面是代码
- 当尝试使用类的实例调用方法时,调试断言错误
- C++:从派生实例调用 base 中的纯虚拟方法重载