is there a way to initialize all datas in a struct at only one statement?

Started by
4 comments, last by mickey 22 years, 2 months ago
for example i have this struct mystruct { bool data1; bool data2; bool data3; bool data4; } well, how will i initialize that all to false in one statement? thanks!
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Advertisement
mystruct ms = { false, false, false, false }; 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
You can do that, but only when you are defining the variable

mystruct a = { false, false, false, false }; // ok
mystruct b;
b = { false, false, false, false }; // not ok

Or, a trick... (works with arrays, maybe not with structs, may not be portable...)

mystruct c = { false }; // all other members implicitely set to zero (i.e. false in this case)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
100% unsafe but it should work for the above case

struct mystruct
{
bool data1;
bool data2;
bool data3;
bool data4;
}

mystruct ms;
memset( &ms.data1, 0, 4 ); // 0 ==false
skip the struct, go with a class
  class CMyClass{   private:      bool data1, data2, data3, data4;   pubilc:   CMyClass();};CMyClass::CMyClass(){   data1=data2=data3=data4=FALSE;}int main(void){   CMyClass a; //all data initialized to FALSE;   return 0;}  

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Disregard above and go with this much more slick solution:
  struct mystruct{bool data1, data2, data3, data4;inline mystruct() { data1=data2=data3=data4=false; }}  

structs can have constructors!

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement