C ++: ctors for structs?

C ++: Since a struct is a class with everything "public", are created and named by default?

I ask you to understand that overhead, if any, in C ++ can have more than C when structures are used. The opinion I heard is that classes have some overhead that are not built in C ++, but I doubt it.

+4
source share
4 answers

In C ++ there are no differences, except for the visible visibility of the elements of the structure by default, it is public, and the members of the class are closed by default.

In terms of performance, building a structure will be as fast as building a class. Actual speed, of course, will depend on what your structure contains. If you move the C structure to C ++, your structure will contain only POD types (plain old data - no classes), which in any case have no constructors.

+15
source

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.

+13
source

Good question! My reading of Straustup is generally consistent with Roddy's answer. However, I think whether the ctor call will to some extent depend on how the structure is created. For example, if you create structures using malloc , I don’t think that the constructor is being called, whereas if you are new , I think this will happen.

However, I did not actually check above.

+1
source

In the book Struustrup, in the C ++ programming language, Special Edition, on page 234, section 10.2.8, he gives an example of a structure with a constructor. He also says a little further in the text “Constructors and access functions can be very useful even for such structures ...” although I do not believe that he used the word “structures” in a strict technical sense. Therefore, I would suggest that the structure has a default constructor.

+1
source

All Articles