Setting data members in an array

Started by
7 comments, last by Zahlman 19 years, 2 months ago
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?
Advertisement
I am no expert, but would a method such as "Insertion" Work?
-Jon
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.
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
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.
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
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.
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.
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


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.


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

This topic is closed to new replies.

Advertisement