C++ C结构和C++结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2242696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:40:42  来源:igfitidea点击:

C structure and C++ structure

c++c

提问by Vijay

Could anybody please tell me what is the main difference between C & C++ structures.

任何人都可以告诉我 C 和 C++ 结构之间的主要区别是什么。

回答by Tronic

In C++ structand classare the exact same thing, except for that struct defaults to publicvisibility and class defaults to privatevisiblity.

在 C++ 中structclass是完全相同的东西,除了结构默认为public可见性和类默认为private可见性。

In C, struct names are in their own namespace, so if you have struct Foo {};, you need to write struct Foo foo;to create a variable of that type, while in C++ you can write just Foo foo;, albeit the C style is also permitted. C programmers usually use typedef struct {} Foo;to allow the C++ syntax for variable definitions.

在 C 中,结构名称在它们自己的命名空间中,因此如果您有struct Foo {};,则需要编写struct Foo foo;以创建该类型的变量,而在 C++ 中,您可以只编写Foo foo;,尽管也允许使用 C 样式。C 程序员通常使用typedef struct {} Foo;允许变量定义的 C++ 语法。

The C programming language also does not support visibility restrictions, member functions or inheritance.

C 编程语言也不支持可见性限制、成员函数或继承。

回答by speeder

In C++, structures behave like classes, allowing methods, constructors, destructors etc...

在 C++ 中,结构的行为类似于类,允许使用方法、构造函数、析构函数等......

The main difference between classes and C++ structures is that everything inside structures is publicby default, while everything inside classes is privateby default. (ie: nothing outside can access them directly)

类和 C++ 结构之间的主要区别在于,默认情况下,结构中的所有内容都是公共的,而类中的所有内容默认都是私有的。(即:外部无法直接访问它们)

回答by Sandeep_black

There are several differences in C and C++ structure

C和C++结构有几个区别

  1. In C we define struct keyword is necessary to create the structure type value while in C++ it is not necessary.

  2. In C there is no function inside the structure while in C++ we can define function that can access the data members of structure directly( Function is names as method in C++ )

  3. There is no concept of access modifier inside the structure in C while in C++ we can find the access modifier (eg. private and public ). By default all are public.

  4. Structure in C can not have static members while in C++ structure can have static members.

  5. Size of empty structure is constraint violation in C, but it is always 1 in C++.

  6. We can have both pointers and references to struct in C++, but only pointers to structs are allowed. (References aren't feature of C language)

  1. 在 C 中,我们定义 struct 关键字是创建结构类型值所必需的,而在 C++ 中则不需要。

  2. 在 C 中,结构内部没有函数,而在 C++ 中,我们可以定义可以直接访问结构数据成员的函数(函数在 C++ 中称为方法)

  3. C 中的结构内部没有访问修饰符的概念,而在 C++ 中我们可以找到访问修饰符(例如 private 和 public )。默认情况下都是公开的。

  4. C 中的结构不能有静态成员,而 C++ 中的结构可以有静态成员。

  5. 空结构的大小在 C 中违反约束,但在 C++ 中始终为 1。

  6. 在 C++ 中,我们可以同时拥有指向结构的指针和引用,但只允许指向结构的指针。(引用不是 C 语言的特性)

回答by Agnel Kurian

In addition to the answers above, remember that C++ structures support inheritance and so can contain pointers to vtables. This can make a big difference when serializing and deserializing these structures across processes. Templates are supported too.

除了上面的答案,请记住 C++ 结构支持继承,因此可以包含指向 vtable 的指针。在跨进程序列化和反序列化这些结构时,这可能会产生很大的不同。也支持模板。

回答by Herat Patel

C : we can't define function inside the structure in c.

C : 我们不能在 c 的结构内定义函数。

C++ : We can define function inside the structure in c++.

C++ : 我们可以在 C++ 的结构内部定义函数。

回答by Anthony Kong

C structs is more akin to a definition of a composite data structure

C 结构更类似于复合数据结构的定义

C++ structs can be thought of as a class but scope of all member variables are defaulted to public.

C++ 结构体可以被认为是一个类,但所有成员变量的范围默认为 public。