structs has a default constructor, under the same circumstances as classes.
By the way, the structure is not a "class with everything public." This is a class with public as the default access specifier. structs may have private members, but your code browser may hit you if they do.
The corresponding problem is not struct vs. class, and POD vs. non-POD.
Remember how a member of the "int" class is not initialized if you did not specify a value in the list of initializers or set a value in the constructor? Well, the same applies to all types of POD. The default constructor for the POD structure (or the POD class, for that matter) has nothing to do. Thus, although it nominally exists, the compiler should not generate and call it.
This means that the default constructor for any type that you could define in C should not enter runtime overhead during use in C ++. In practice, I hear rumors that not all C ++ compilers apply enough optimization to ensure that all the code always emits the binary as good as it would if it were compiled as C. But I don't know if this is ever one of the problems causing the problems - I would suggest that it is usually identical.
So, given the structure of the POD:
struct sPOD { int foo; float bar; char baz[23]; };
In C ++, there may be no code (other than moving the stack pointer, depending on how much the compiler copies the automatic variables together), as in C:
sPOD s1;
Indeed, this is not for me on gcc.
source share