简体   繁体   English

如何在C ++中重置结构中的所有变量?

[英]How to reset all variables in a struct in C++?

I know how to reset the variable manually one by one, but is their anyway I could call the struct function to re-read the menu(): part? 我知道如何手动一个个地重置变量,但是无论如何我可以调用struct函数来重新读取menu():part吗? Because those variables are already by default. 因为这些变量已经是默认设置。 Or do I have to reset them one by one myself? 还是我必须自己一个个重置它们?

struct menu 
{
    bool update;
    bool enterPressed;
    int scroll;
    int choice;
    int type;

    menu(): update(true), enterPressed(false), scroll(0), choice(0), type(0) {}
} menu;

Easiest way with the above code is to copy from a new instance, eg: 上面的代码最简单的方法是从新实例复制,例如:

menu blah;
// reset blah to defaults
blah = menu();

With C++11, this will work: 使用C ++ 11,这将起作用:

menu m;
// ...
m = {};

This will create a temporary menu and assign it to m . 这将创建一个临时menu并将其分配给m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM