C++ 使用结构的所有实例调用函数
我想用所有结构对象调用一个函数 我需要一个函数,它可以通过只调用struct struct_a来循环遍历struct对象a_1,a_2。代码底部的空函数“reset_all_structs(??)” 示例代码:C++ 使用结构的所有实例调用函数,c++,function,struct,C++,Function,Struct,我想用所有结构对象调用一个函数 我需要一个函数,它可以通过只调用struct struct_a来循环遍历struct对象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: ma
#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;
};
#包括
结构体{
无符号字符数=0;
bool bool_1=0;
bool bool_2=0;
}A_1,A_2;//对象:也许以后A_3,A_x
无效打印到终端(结构A和结构名称);
无效集合1(结构名称和结构名称);
无效集合2(结构名称和结构名称);
无效重置一个结构(结构名称和结构名称);
void reset_all_structs();
int main()
{
集合布尔1(A_1);
A_1.number=111;
集合布尔2(A_2);
A_2.number=222;
std::cout您可以将它们放在本地数组中并在其上循环,如下所示:
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]);
}
}
void reset_all_structs()
{
//此数组可以在本地创建,并且必须通过指针捕获
//引用数组在C++中是非法的。
STRUCT_A*structs[]={&A_1和&A_2};
对于(int i=0;i
顺便说一下,你把问题加在C++上,但是这看起来像C.,如果你是C++的新手,我建议你把所有的函数都用在结构中,把结构名命名成方法> /Cuff>。最后,我建议将您的结构实例收集到一个STL容器中,如std::vector
,以简化循环中的迭代和访问。使用数组
,或者更好地使用结构的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);
struct结构{
无符号字符数=0;
bool bool_1=0;
bool bool_2=0;
};
类型定义结构A数据;
无效集1(数据和数据集1){
数据_i.bool_1=1;
}
无效集2(数据和数据集1){
数据_i.bool_1=1;
}
无效重置结构(数据和数据){
数据_i.bool_1=0;
data_i.bool_2=0;
数据i.number=0;
}
无效重置所有结构(标准::向量和所有数据){
对于(int i=0;i
语言本身不可能遍历相同类型的相同变量
您可以创建结构的某种注册表,然后遍历该注册表。类似于:
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;
}
}
};
struct\u A;
std::vector struct_A_注册表;
结构体{
...
//建造师
STRUCT_A(){STRUCT_A_registry.push_back(this);}
//析构函数
~STRUCT_A(){
for(std::vector::iterator it=struct_A_registry.begin();
it!=struct_A_registry.end();+it)
如果(*it==此){
结构注册表。擦除(此);
打破
}
}
};
然后你的重置所有结构将只是
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);
}
void reset_all_structs(){
for(std::vector::iterator it=struct_A_registry.begin();
it!=struct_A_registry.end();+it)
重置一个结构(**it);
}
或者,如果您在程序的同一行声明所有结构变量,您可以将它们组织到一个数组中,这会简单得多。如果您的设计使用了这些工具,您使用的语言具有处理这种情况的工具。请阅读有关容器(如std::vector)的信息,这些容器可用于操作一个或更多结构。