Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 mickey   Members   -  Reputation: 100

Like
Likes
Like

Posted 28 January 2002 - 08:08 PM

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!

Sponsor:

#2 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 28 January 2002 - 08:13 PM

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!


#3 Fruny   Moderators   -  Reputation: 1653

Like
Likes
Like

Posted 28 January 2002 - 08:15 PM

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)

#4 zedzeek   Members   -  Reputation: 528

Like
Likes
Like

Posted 28 January 2002 - 10:12 PM

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

#5 Sean T. McBeth   Crossbones+   -  Reputation: 1632

Like
Likes
Like

Posted 29 January 2002 - 03:21 AM

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;
}


#6 Promit   Moderators   -  Reputation: 7572

Like
Likes
Like

Posted 29 January 2002 - 03:24 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS