简体   繁体   English

C ++:ctors for structs?

[英]C++: ctors for structs?

C++: Since a struct is a class with everything "public", are default -ctors created and called? C ++:由于struct是一个包含所有“public”的类,是否创建并调用了default -ctors?

The reason I ask is to understand the overhead, if any, that C++ may have over C, when structs are used. 我要问的原因是当使用结构时,理解C ++可能具有的开销(如果有的话)。 An opinion I have heard is that classes have some overhead that structs don't, in C++, but I question this. 我听到的一个观点是,在C ++中,类有一些结构没有结构的开销,但我对此提出质疑。

In C++ there's no difference except the default visibilty of struct members is public, while class members default to private. 在C ++中没有区别,除了struct member的默认visibilty是public,而class members默认为private。

In terms of performance, struct construction will be just as fast as class construction. 在性能方面,结构构造将与类构造一样快。 Actual speed will of course depend on what your struct contains. 实际速度当然取决于结构包含的内容。 If you are moving a C struct to C++, your struct will only contain POD types (plain old data - no classes), which don't have constructors anyway. 如果要将C结构移动到C ++,那么结构将只包含POD类型(普通旧数据 - 没有类),它们无论如何都没有构造函数。

structs do have a default constructor, under exactly the same circumstances that classes do. 在类完全相同的情况下,结构体确实有一个默认的构造函数。

By the way, a struct isn't "a class with everything public". 顺便说一句,结构不是“一个公开的一类”。 It's a class with public as the default access specifier. 它是一个以public作为默认访问说明符的类。 structs can have private members, but your code reviewer might punch you if they do. 结构可以有私有成员,但是如果他们这样做,你的代码审查者可能会打你。

The relevant issue isn't struct vs. class, it's POD vs. non-POD. 相关问题不是结构与类,它是POD与非POD。

Remember how an "int" member of a class is uninitialized unless you give it a value in the initializer list or set a value in the constructor? 还记得一个类的“int”成员是如何未初始化的,除非你在初始化列表中给它一个值或在构造函数中设置一个值? Well, the same applies to all POD types. 嗯,这同样适用于所有POD类型。 The default constructor for a POD struct (or a POD class, for that matter) has nothing to do. POD结构(或POD类)的默认构造函数无关。 So, although it nominally exists, a compiler shouldn't actually generate and call it. 因此,虽然名义上存在,但编译器实际上不应该生成并调用它。

This means that the default constructor for any type that you could have defined in C, should introduce no runtime overhead when used in C++. 这意味着您可以在C中定义的任何类型的默认构造函数在C ++中使用时不应引入运行时开销。 In practice, I hear rumours that not all C++ compilers apply enough optimisation to ensure that all code always emits a binary as good as it would have done if compiled as C. But I don't know whether this is ever one of the issues causing problems - I'd guess that it's usually identical. 在实践中,我听到有传言说并非所有的C ++编译器都应用足够的优化来确保所有代码总是发送一个二进制文件,就像编译为C一样好。但是我不知道这是否是引起问题的原因之一问题 - 我猜它通常是相同的。

So, given a POD struct: 所以,给定一个POD结构:

struct sPOD {
    int foo;
    float bar;
    char baz[23];
};

The following is likely to emit no code in C++ (other than maybe moving the stack pointer, depending how much the compiler rolls automatic variables together), just like in C: 以下内容可能不会在C ++中发出任何代码(除了可能会移动堆栈指针,这取决于编译器将自动变量放在一起的程度),就像在C中一样:

sPOD s1;

Indeed it doesn't for me on gcc. 事实上,它不适合我在gcc上。

Good question! 好问题! My reading of Stroustrup would tend agree with Roddy's answer. 我对Stroustrup的阅读倾向于赞同Roddy的回答。 However, I think whether or not the ctor is called depends to a certain extent on how the struct is created. 但是, 我认为是否调用ctor在某种程度上取决于结构的创建方式。 For example, if you create the structs via malloc , I don't believe the constructor is called, whereas if you new them, I guess it would. 例如,如果你通过malloc创建结构,我不相信构造函数被调用,而如果你新建它们,我想它会。

That said, I haven't actually checked the above. 那就是说,我实际上没有检查过上面的内容。

In Stroustrup's book, The C++ Programming Language, Special Edition , on page 234 section 10.2.8, he gives an example of a struct with a constructor. 在Stroustrup的书“C ++编程语言,特别版” (第234页,第10.2.8节)中,他给出了一个带构造函数的结构示例。 He also says a little further down in the text "Constructors and access functions can be quite useful even for such structures . . ." 他还在文本中进一步说明“构造函数和访问函数即使对于这样的结构也非常有用......” although I don't believe he's using the word "structures" in the strict technical sense. 虽然我不相信他在严格的技术意义上使用“结构”这个词。 So I would guess that a struct does have a default constructor. 所以我猜测结构确实有一个默认的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM