Shortest and best way to “reinitialize”/clean a class instance

后端 未结 5 1916
感动是毒
感动是毒 2021-01-02 01:56

I will keep it short and just show you a code example:

class myClass
{
public:
  myClass();
  int a;
  int b;
  int c;
}

// In the myClass.cpp or whatever
m         


        
5条回答
  •  梦毁少年i
    2021-01-02 02:28

    Just assign to a default-constructed class, like you have. Just use a temporary, though:

    struct foo
    {
        int a, b, c;
    
        foo() :
        a(), b(), c()
        {} // use initializer lists
    };
    
    foo f;
    f.a = f.b =f.c = 1;
    
    f = foo(); // reset
    

提交回复
热议问题