为什么在C ++中有一个类关键字? [英] Why is there a class keyword in C++?

查看:97
本文介绍了为什么在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++.

这篇关于为什么在C ++中有一个类关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆