Why is there a class keyword in C++?

安稳与你 提交于 2019-12-17 20:43:37

问题


This question came to my mind when I learned C++ with a background of C. Even if there was a struct why did Stroustrup felt it was necessary to introduce the class keyword? I tried asking people at that time but couldn't get a satisfactory answer. So can the Stack Overflow community answer it?


回答1:


As David says, structs are public by default, classes are private by default. The larger point is that adding object orientation to C was a big change, and giving developers ways to express themselves accurately is an important part of designing a language.

As it turns out, the distinction between struct and class is quite minor from a technical point (default-public vs default-private), but in programmers' minds, the distinction is quite large. Adding the keyword was an important way to emphasize the OO nature of C++.




回答2:


In The Design and Evolution of C++, while describing how C++'s object model and virtual functions were developed, he writes (p. 76):

At this point, the object model becomes real in the sense that an object is more than the simple aggregation fo the data members of a class. An object of a C++ class with a virtual function is a fundamentally different beast from a simple C `struct`. Then why did I not at this point choose to make structs and classes different notions?

My intent was to have a single concept: a single set of layout rules, a single set of lookup rules, a single set of resolution rules, etc... I was convinced that if `struct` came to mean "C and compatibility" to users and `class` came to mean "C++ and advanced features," the community would fall into two distinct camps that would soon stop communicating. Beingn able to use as many or as few language features as needed when designing a class was an important idea to me. Only a single concept would support my ideas of a smooth and gradual transition from "traditional C-style programming," through class abstraction, to object-oriented programming. Only a single concept would support this notion of "you only pay for what you use" ideal.

So it sounds like the class keyword was introduced to indicate C++-specific object orientation and then its compatibility with the struct keyword was introduced.




回答3:


Originally, C++ was called "C with classes"

EDIT:
Although the following speculation is plausible, the reason for the two keywords was probably practical in nature: by keeping the syntax and semantics of the struct backward compatible, it was possible to introduce C++ into existing programs easily (as opposed to say revisit all structs and add the keyword 'public' to them...).

[Speculation] The fact that me have two keywords, may possibly be associated with the genesis of the new languge, whereby initially the OO features were exclusively associated with the new keyword, "class". As this matured it was decided that it would be convenient to introduce some OO features to structs as well, and to keep these...

...two concepts for two different uses:

  • struct : for typically small, and data-only "objects", with its members public by default. (but which can also be less transparent and have behavior as well).
  • class : for objects typically grouping data and behavior (functions) and with its members private by default, to implement data-hiding, encapsulation and other OO features.

The struct is for fully or mostly transparent "objects" with no/little data hiding or behavior, in a continuation of its non-object-oriented use (although such transparent constructs have their place in the the broader context of OO programs). Whereby classes, were intended for introducing of data-hiding and other OO practices.

An alternative may have been to use the struct keyword for both usages, requiring programmers intending it in its 'class' sense to explicitly define the private members. At a time when OO concepts were not broadly understood in the programmers' community (cf other responses in this post) it was probably felt that a separate keyword would better help 'socialize' the new features/concepts.




回答4:


The literal answer has already been given: "struct members are public by default, classes members are private by default". And most programmers explicitly identify all their sections as private, protected or public anyway.

So literally speaking there's nothing you can do with a class you can't do with a typedef struct.

However, a lot of C++ users use the "struct" to also identify C++ entities that are "plain old data structures". The idea of PODS is a very useful C++ concept - PODS are easily constructed, copied, and "have no surprises", while classes are much more dangerous.

For example, PODS can be initialized into arrays at compilation time, which makes them very useful indeed for static tables.

So I strongly recommend you follow the same convention - structs are PODS, anything else is a class.




回答5:


Bjarne Stroustrup, the original designer of C++, was strongly inspired by the Simula programming language. That is where he got the class concept and the name "class".

As indicated in the answer which quoted "Design and Evolution", the real question Bjarne asked himself wasn't "Why introduce a class keyword?". Instead he asked "Should a struct behave similarly to a class?". Bjarne decided that they should. This article from 1997 argues that he made the wrong decision.



来源:https://stackoverflow.com/questions/1654444/why-is-there-a-class-keyword-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!