Jump to content
  • Advertisement
Sign in to follow this  
SirSmokey

Setting data members in an array

This topic is 4908 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

Is there a way I can set the value of a data member in an array of classes without using a for loop? Example: If I have-> CObject Object[10] Instead of looping through the array can I use something like memset to set the value of Object.Data in all ten elements?

Share this post


Link to post
Share on other sites
Advertisement
Yes, in theory you could use memset. However, since memset only takes a char as the value to set the memory to, in practice this is normally only useful for initializing everything in your objects to zero.

Share this post


Link to post
Share on other sites
Well, you could set the value in the constructer of CObject:

CObject::CObject()
{
member_variable = 5;
}

and when the array is initialized, member_variable for each CObject[] is set to 5.

this has its limitations though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmadog
Yes, in theory you could use memset. However, since memset only takes a char as the value to set the memory to, in practice this is normally only useful for initializing everything in your objects to zero.


This is still a very bad idea. The data layout of classes/structures in memory is not yours to control. In particular, if the class uses any kind of polymorphism, this would nuke the vtables.

Anyway, yes. You should be relying on the default constructor to specify default contents of objects.

Share this post


Link to post
Share on other sites
I wouldn't, especially if it breaks the default construction of other instances. And you can't, alas, explicitly specify constructor parameters for an array's elements. Looping through is the best option. IIRC, placement new may offer some [hacky] joy.

Share this post


Link to post
Share on other sites
Quote:
Original post by geekalert
Well, you could set the value in the constructer of CObject:

CObject::CObject()
{
member_variable = 5;
}

and when the array is initialized, member_variable for each CObject[] is set to 5.

this has its limitations though.


Use this instead:


CObject::CObject()
:
member_variable(5)
{
}




This way the variable will be created when the object is created, not set afterwards...

Another tip is to use std::vector, then you can use a number of stl-algorithms on it, such as "fill" in your case.

/.tnan

Share this post


Link to post
Share on other sites


You have to iterate it in some way to set individual fields. The compiler will not be able to generate code without knowing what the field is and where it is in memory. A memset just blasts a chunk of memory with a single value and it will destroy your objects.


Share this post


Link to post
Share on other sites
Quote:
Original post by CJH
I wouldn't, especially if it breaks the default construction of other instances.


I would say that if "default construction" would more conveniently mean different things in different places, then you may want to rethink the design of the object entirely... :s

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!