Never use structs without proper constructors
structs are legal C++ constructs, used to aggregate data together. Still, the data should be always properly initialized.
All C++ structs should have at least a default constructor, which will set its aggregated data to default values.
struct MyStruct // BAD
{
int i ; bool j ; char * k ;
}
struct MyStruct // GOOD
{
MyStruct() : i(0), j(true), k(NULL) : {}
int i ; bool j ; char * k ;
}
And if they are usually initialized in some way, provide a constructor to enable the user to avoid a C-style struct initialization:
MyStruct oMyStruct = { 25, true, "Hello" } ; // BAD
MyStruct oMyStruct(25, true, "Hello") ; // GOOD
How it clearly facilitates safer code, which minimizes the risk of enigmatic bugs, which increases maintainability, etc.?
Having struct without a proper constructor leaves the user of this struct the task of initializing it. So, the following code will be copy pasted from function to function:
void doSomething()
{
MyStruct s = { 25, true, "Hello" } ;
// Etc.
}
void doSomethingElse()
{
MyStruct s = { 25, true, "Hello" } ;
// Etc.
}
// Etc.
Which means that, in C++, if you need to add a field in the struct, or change the order of the internal data, you have to go through all these initializations to verify each is still correct. With a proper constructor, modifying the internals of the structs is decoupled from its use.