Archived

This topic is now archived and is closed to further replies.

Default values for structures

This topic is 5318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you set default values in structures? Especially arrays in structures. I''ve been looking at classes and constructors in particular. Could I use a constructor to initialize default values? My structure is pretty big so I don''t want to specify every default value when I create an instance of it. Is there another way? Also, the C++ compiler won''t let me because it says it''s a non-allegated structure or something. Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by de_matt
How do you set default values in structures? Especially arrays in structures.



There are several ways.

Yes, you could create a constructor if you want. But it''s uncommon, as if you''re going to start making constructors, etc, most people think "class". (Although technically the only difference between a class and a struct is classes default to private, and structs default to public).

If you want to set all values to 0, you could use:

memset(&mystruct, 0, sizeof(mystruct));
or
ZeroMemory(&mystruct, sizeof(mystruct));

If you have an array:

MyStruct mystructs[NUM_MYSTRUCTS];
ZeroMemory(mystructs, sizeof(mystructs[0]) * NUM_MYSTRUCTS);

Or you can hard-code everything:

MyStruct mystructs[NUM_MYSTRUCTS] =
{
{ 1, 2, 0.5f, true},
{ 4, 6, 0.4f, true},
{ 1, 2, 0.3f, true},
{ 5, 7, 4.5f, false},
{ 1, 12, 4.5f, true}
}

Share this post


Link to post
Share on other sites
quote:
Original post by BriTeg
If you want to set all values to 0, you could use:

memset(&mystruct, 0, sizeof(mystruct));
or
ZeroMemory(&mystruct, sizeof(mystruct));

If you have an array:

MyStruct mystructs[NUM_MYSTRUCTS];
ZeroMemory(mystructs, sizeof(mystructs[0]) * NUM_MYSTRUCTS);



I believe this isn''t required to produce the expected result for floats, doubles, and pointers.

quote:

Or you can hard-code everything:

MyStruct mystructs[NUM_MYSTRUCTS] =
{
{ 1, 2, 0.5f, true},
{ 4, 6, 0.4f, true},
{ 1, 2, 0.3f, true},
{ 5, 7, 4.5f, false},
{ 1, 12, 4.5f, true}
}




I know that some version of C (maybe C++?) allows you to do something like

MyStruct mystructs[NUM_MYSTRUCTS] =
{
{0}, {0}, {0}, ..., {0}
};

and that will initialize each element as if you had set each one equal to 0 (i.e. produce the expected result for floats, doubles, and pointers)

Share this post


Link to post
Share on other sites
quote:
Original post by Way Walker
I believe this isn''t required to produce the expected result for floats, doubles, and pointers.



That may be, a quick test indicates you''re probably right. But I don''t like seeing uninitialized variables (or what looks like uninitialized variables, even if the compiler is initializing them for me), so I always set everything to 0 (or what I need) manually right off the bat.

quote:

I know that some version of C (maybe C++?) allows you to do something like

MyStruct mystructs[NUM_MYSTRUCTS] =
{
{0}, {0}, {0}, ..., {0}
};

and that will initialize each element as if you had set each one equal to 0



Yes, that should work too, I forgot about that way.

Share this post


Link to post
Share on other sites
I''d suggest you use a constructor, as it''s much cleaner IMHO. I use lots of structs with constructors, and I don''t think the "people will think class" argument counts very much. But that''s just my opinion...


My Wonderful Web Site (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

Share this post


Link to post
Share on other sites
quote:
Original post by randomZ
I use lots of structs with constructors, and I don''t think the "people will think class" argument counts very much.



Maybe you''re right, considering the average age of programmers in this forum. Us old-timers though, who programmed in C before C++ was mainstream, just feel weird putting a constructor or any member functions in a struct.

Share this post


Link to post
Share on other sites
Why not make everything a whole lot easier and just make it a class instead of a struct?...There are alot of people nowadays who believe there is absolutely no reason to use structs in c++, ever.

unkn.Enigma1625

Share this post


Link to post
Share on other sites
quote:
Original post by BriTeg
Maybe you''re right, considering the average age of programmers in this forum. Us old-timers though, who programmed in C before C++ was mainstream, just feel weird putting a constructor or any member functions in a struct.



Yeah, I''m 16 and open to new ideas
Well, actually, ctors and dtors are already where I draw the line with structs.

I think of a (instance of a) class as a "living thing", something that can do things on its own. A constructor which only initializes some default values therefore doesn''t make a struct (just some grouped-together variables, so-to-speak) a class.

This topic is, however, too dependend on personal taste and preferences to really have a point.


My Wonderful Web Site (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

Share this post


Link to post
Share on other sites
quote:
Original post by BriTeg
Us old-timers though, who programmed in C before C++ was mainstream, just feel weird putting a constructor or any member functions in a struct.

Speak for yourself!

Share this post


Link to post
Share on other sites