Reputation:
With structure :
struct element_data
{
int size;
int type;
int nID;
int attribute;
double fValue;
int mode;
bool bTemp;
};
...I have to manually initialize all members with zero :
inline element_data() : //Empty constructor
size(0),
type(0),
nID(0),
attribute(0),
fValue(0),
mode(0),
bTemp(false)
{}
Too many commands, that may slowdown my calculator. Is there any other way which can do this? For example with a single command all values will take the zero values...
Upvotes: 0
Views: 115
Reputation: 44448
No. There isn't. At least, if efficiency is your concern.
I quote, from the above link, "All other things being equal, your code will run faster if you use initialization lists rather than assignment."
Remember, just because it's a lot of code, doesn't mean its less efficient. In fact, optimized code usually tends to look larger and more complex than unoptimized, general purpose code. This is usually because you're taking short cuts based on the properties of the data you're processing or on the algorithm you're using.
The advantage with this approach is that it does not rely on the code that creates the instance to correctly initialize the members to zero, while being efficient too. An initializer list makes sure your structure members are always initialized correctly.
Upvotes: 1
Reputation: 45410
As your struct element_data is POD you could initialize members to default value in two ways:
element_data ed = element_data();
OR
element_data ed = {};
All integer members will be initialized to 0
and bool will be initialized to false
.
Upvotes: 0