本文介绍了是否准确地说,C ++是一个部分类型安全的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的一些功能是类型安全的,而其他一些功能则不是。

There are some features in C++ that are type safe and some other features that are not.

C ++类型安全性示例:

Example of C++ type safety:

char c = 'a';
int *p = &c;    // this is not allowed (compiler error)

C ++缺少类型安全性的示例:

Example of C++ lack of type safety:

int *p;        // I don't have to initialize p, and so it will have some junk/random value
*p = 12345;    // this will probably lead to segmentation fault!

我读了几篇文章说C ++不是类型安全的语言。我可以认为它是一个非类型安全的语言,或者更准确地说它是一个部分类型安全的语言?

I have read in a couple of articles that C++ is not a type safe language. Can I consider it to be a non type safe language, or is it more accurate to say that it is a partially type safe language?

推荐答案

这是合理几乎有意义说C ++是一种部分类型安全的语言。

It's reasonable and practically meaningful to say that C++ is a partially type safe language.

作为原始中期到1970年代末C的扩展,它被设计为一种高级便携式汇编语言,使Unix更便携,更易于维护。 C ++为其新功能添加了类型安全性,但目标是主要与C兼容(特别是使用所有现有的C库,包括其头文件),C的原始核心必须保留原样。

C++ started as an extension of original mid- to late 1970's C, which was designed as a kind of high level portable assembly language, to make Unix more portable and easier to maintain. C++ added type safety for its new features, but with the goal of being mainly compatible with C (in particular using all those existing C libraries, including their headers) the original core of C had to be left as it was.

特别是,C ++得到了数组到C指针的衰减。在C ++中,它不是类型安全的,因为它允许从 Derived 指向 Base 的指针,它可以依次被索引但具有未定义的行为。

In particular, C++ got the decay of array to pointer from C. In C++ it isn't type safe because it allows an implicit conversion from array of Derived to pointer to Base, which can in turn be indexed but with Undefined Behavior.

这篇关于是否准确地说,C ++是一个部分类型安全的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:13