C++ 完全擦除(或重置)结构的所有值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15183429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:08:12  来源:igfitidea点击:

C++ completely erase (or reset) all values of a struct?

c++

提问by user2117427

So, I was just wondering how could we completely erase or reset a structure so it could be reused?

所以,我只是想知道我们怎样才能完全擦除或重置一个结构,以便它可以重复使用?

I just typed this up, here you go:

我刚刚输入了这个,给你:

typedef struct PART_STRUCT
{
    unsigned int Id;
    std::string Label;
} Part;

typedef struct OBJECT_STRUCT
{
    std::vector< unsigned char > DBA;
    std::vector< Part > Parts;
    unsigned int Id;
} Object;

Object Engine;

// Initialize all members of Engine
// Do whatever with Engine
// ...
// Erase/Reset Engine value

回答by Pubby

You can just assign a constructed temporary to it:

您可以为其分配一个构造的临时文件:

Part my_struct;

my_struct = Part(); // reset

C++11:

C++11:

my_struct = {}; // reset

回答by Rapptz

If for some reason I was hell-bent on keeping the same object constantly, I would just write a resetmethod that would reset all the values back to what they were.

如果出于某种原因,我一心要始终保持同一个对象,我只会编写一个reset方法,将所有值重置回原来的状态。

Something similar to this:

类似的东西:

struct Test {
    int a;
    int b;
    Test(): a(0), b(100) {}
    void reset() {
        a = 0;
        b = 100;
    }
};

int main() {
    Test test;
    //do stuff with test
    test.reset(); //reset test
}

回答by Qortex

Good practice is to avoid that type of construct (using the same variable for two different semantics meanings, having reset it in the meantime). It will inevitably create a weird bug later on when you (or somebody else) modify your code and forgets you shared a variable for two different uses.

好的做法是避免这种类型的构造(对两种不同的语义使用相同的变量,同时对其进行重置)。当您(或其他人)修改您的代码并忘记您共享一个变量用于两种不同用途时,它将不可避免地产生一个奇怪的错误。

The only justification would be to spare some memory space but:

唯一的理由是节省一些内存空间,但是:

  • It is very unlikely that you actually need such an optimisation.
  • Even if you do, the compiler will usually figure out a variable on the stack is no longer use and can be discarded, the new variable you would create will thus effectively replace the first one. You do not need to care about sparing the memory yourself.
  • If your variables are on the heap, you are better off just using two different pointers.
  • 您实际上不太可能需要这样的优化。
  • 即使这样做,编译器通常会发现堆栈上的变量不再使用并且可以丢弃,因此您将创建的新变量将有效地替换第一个变量。您无需关心自己保留内存。
  • 如果变量在堆上,最好只使用两个不同的指针。

But if you really want to do this reset, you must write a method to do it. There is not built-in way in C++, because it would actually require calling the destructorand then the constructoragain.

但是如果你真的想做这个重置,你必须写一个方法来做。C++ 中没有内置方式,因为它实际上需要调用destructor,然后constructor再次调用。

The solution my_struct = Part()works only if your destructoris trivial. Let's say you have allocated pointer in your std::vector, you would have to properly deleteevery pointer before emptying the vector. That's why it cannot be done automatically: the cleanup of the structure may require special treatment rather than plain forgetting.

该解决方案my_struct = Part()仅在您destructor微不足道时才有效。假设您已经在您的 中分配了指针std::vector,您必须delete在清空vector. 这就是为什么它不能自动完成:结构的清理可能需要特殊处理而不是简单的遗忘。