如何在 C++ 中重置结构中的所有变量?
How to reset all variables in a struct in C++?
我知道如何一个一个地手动重置变量,但无论如何我可以调用结构函数来重新读取 menu(): 部分吗?因为这些变量已经默认了。还是必须自己一一重置?
struct menu
{
bool update;
bool enterPressed;
int scroll;
int choice;
int type;
menu(): update(true), enterPressed(false), scroll(0), choice(0), type(0) {}
} menu;
上述代码最简单的方法是从新实例复制,例如:
menu blah;
// reset blah to defaults
blah = menu();
对于 C++11,这将有效:
menu m;
// ...
m = {};
这将创建一个临时 menu
并将其分配给 m
。
我知道如何一个一个地手动重置变量,但无论如何我可以调用结构函数来重新读取 menu(): 部分吗?因为这些变量已经默认了。还是必须自己一一重置?
struct menu
{
bool update;
bool enterPressed;
int scroll;
int choice;
int type;
menu(): update(true), enterPressed(false), scroll(0), choice(0), type(0) {}
} menu;
上述代码最简单的方法是从新实例复制,例如:
menu blah;
// reset blah to defaults
blah = menu();
对于 C++11,这将有效:
menu m;
// ...
m = {};
这将创建一个临时 menu
并将其分配给 m
。