C++ struct 可以有成员函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24196885/
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
Can C++ struct have member functions?
提问by Sam
I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default. However, my lecturers have just told me that structs cannot contain member functions. But I have seen many threads on the internet where people include member functions in structs and specifically say that it is alright to do so.
我对 struct 和 class 之间的区别感到非常困惑,因为我似乎看到它们用于几乎相同的事情。我在谷歌上搜索了差异,我看到的唯一答案是默认情况下结构具有公共成员,默认情况下类具有私有成员。然而,我的讲师刚刚告诉我结构不能包含成员函数。但是我在互联网上看到很多线程,人们在结构中包含成员函数,并特别说这样做是可以的。
My lecturers seem adamant that structs by definition cannot have functions, so what is going on? The only thing I could think of is that maybe the compiler changes functions within a struct to something else so that they technically don't contain functions... Is there a clear answer to these contradictions?
我的讲师们似乎坚持根据定义结构不能有函数,所以这是怎么回事?我唯一能想到的是,也许编译器将结构中的函数更改为其他函数,以便它们在技术上不包含函数......这些矛盾有明确的答案吗?
回答by Brian
I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default.
我在谷歌上搜索了差异,我看到的唯一答案是默认情况下结构具有公共成员,默认情况下类具有私有成员。
Yes, this is correct. In addition, bases of a struct are inherited publicly by default, whereas bases of a class are inherited privately by default.
是的,这是正确的。此外,默认情况下,结构的基类是公开继承的,而类的基类默认是私有继承的。
Declaring a function as a member of a struct has precisely the same semantics as declaring a function as a member of a class, except for the difference you've noted. In each case they are called member functions.
将函数声明为结构的成员与将函数声明为类的成员具有完全相同的语义,除了您已经注意到的不同之处。在每种情况下,它们都称为成员函数。
回答by meagar
Your lecturers are demonstrably wrong, assuming they are actually talking about C++ and not C.
你的讲师显然是错误的,假设他们实际上是在谈论 C++ 而不是 C。
It's trivially easy to prove this to yourself in code, or pick up The C++ Programming Languageand look up structs.
在代码中向自己证明这一点非常容易,或者拿起C++ 编程语言并查找结构。
回答by Patrick Collins
C++ structs
can definitely have member functions. C structs, on the other hand, are a much different beast -- they're essentially arrays that provide names and type information for certain indicies.
C++绝对structs
可以有成员函数。另一方面,C 结构是一个非常不同的野兽——它们本质上是为某些索引提供名称和类型信息的数组。
回答by Travis
It seems to me that just because something is allowed by a language does NOT mean it is a good idea. Although technically a C++ struct can contain functions, I don't think the original intent of the struct data type was to contain functions. Was it not the main purpose of introducing a Class type to support defining the data and the logic that acts on the data in a close knit relationship, thus promoting healthier interfaces to the data?
在我看来,仅仅因为某种语言允许某事并不意味着它是一个好主意。尽管从技术上讲,C++ 结构体可以包含函数,但我认为结构体数据类型的初衷并不是要包含函数。引入 Class 类型来支持定义数据和以紧密联系的方式作用于数据的逻辑,从而促进更健康的数据接口难道不是主要目的吗?
回答by YuGiOhJCJ
In the C++98 standard:
在 C++98 标准中:
A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public by default (clause 11).
结构体是用类键结构体定义的类;它的成员和基类(第 10 条)默认是公开的(第 11 条)。
and
和
Members of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.
默认情况下,使用关键字 class 定义的类的成员是私有的。默认情况下,使用关键字 struct 或 union 定义的类的成员是公共的。
So it means that the only difference between struct
and class
is the default member access control that is public
or private
.
所以这意味着struct
和之间的唯一区别class
是默认成员访问控制是public
或private
。
Can C++ struct have member functions?
C++ struct 可以有成员函数吗?
Yes, they can.
是的他们可以。
My lecturers seem adamant that structs by definition cannot have functions, so what is going on?
我的讲师们似乎坚持根据定义结构不能有函数,所以这是怎么回事?
If it is a lecture on C, it is correct. If it is a lecture on C++, it is not correct.
如果是关于 C 的讲座,则是正确的。如果是关于C++的讲座,那是不正确的。
The only thing I could think of is that maybe the compiler changes functions within a struct to something else so that they technically don't contain functions... Is there a clear answer to these contradictions?
我唯一能想到的是,也许编译器将结构中的函数更改为其他函数,以便它们在技术上不包含函数......这些矛盾有明确的答案吗?
Yes, there is a clear answer: C++ struct can have member functions.
是的,有一个明确的答案:C++ struct 可以有成员函数。
回答by NAREK ZAMANYAN
I am currently working on a project, and I just realized that my struct needs to be able to provide a comparison operators in order for the sorted list that contains it to be able to do the sorting. So it makes sense to have operator overloading in a struct. That's the only legitimate reason to have a struct with methods.
我目前正在做一个项目,我刚刚意识到我的结构需要能够提供一个比较运算符,以便包含它的排序列表能够进行排序。因此,在结构中重载运算符是有意义的。这是拥有带有方法的结构的唯一合法理由。