为什么在C ++中有一个类关键字?
当我在C的背景下学习C ++时,这个问题出现在我脑海中。即使有一个 struct
为什么 Stroustrup 认为有必要介绍 class
关键字?我试着问当时的人,但不能得到满意的答案。因此,Stack Overflow社区会回答这个问题吗?
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?
如David所说,结构默认是public,默认是私有的。更大的一点是,将对象定向添加到C是一个很大的变化,给开发人员准确表达自己的方法是设计语言的重要部分。
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.
事实证明,结构和类之间的区别从技术点(default-public vs default-private)是很小的,但在程序员的心中,区别很大。添加关键字是强调C ++的OO性质的重要方式。
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++.