is there a way to initialize all datas in a struct at only one statement?
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!
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)
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)
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
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;}
Disregard above and go with this much more slick solution:
structs can have constructors!
-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement