- Hands-On Embedded Programming with C++17
- Maya Posch
- 397字
- 2021-08-20 10:20:46
Strongly typed
Type information is necessary to test for proper access to and interpretation of data. A big feature in C++ that's relative to C is the inclusion of a strong type system. This means that many type checks performed by the compiler are significantly more strict than what would be allowed with C, which is a weakly typed language.
This is mostly apparent when looking at this legal C code, which will generate an error when compiled as C++:
void* pointer; int* number = pointer;
Alternatively, they can also be written in the following way:
int* number = malloc(sizeof(int) * 5);
C++ forbids implicit casts, requiring these examples to be written as follows:
void* pointer; int* number = (int*) pointer;
They can also be written in the following way:
int* number = (int*) malloc(sizeof(int) * 5);
As we explicitly specify the type we are casting to, we can rest assured that during compile time any type casts do what we expect them to do.
Similarly, the compiler will also complain and throw an error if we were to try to assign to a variable with a const qualifier from a reference without this qualifier:
const int constNumber = 42; int number = &constNumber; // Error: invalid initialization of reference.
To work around this, you are required to explicitly cast the following conversion:
const int constNumber = 42; int number = const_cast<int&>(constNumber);
Performing an explicit cast like this is definitely possible and valid. It may also cause immense issues and headaches later on when using this reference to modify the contents of the supposedly constant value. By the time you find yourself writing code like the preceding, however, it can reasonably be assumed that you are aware of the implications.
Such enforcement of explicit types has the significant benefit of making static analysis far more useful and effective than it is in a weakly typed language. This, in turn, benefits run-time safety, as any conversions and assignments are most likely to be safe and without unexpected side effects.
As a type system is predominantly a feature of the compiler rather than any kind of run-time code, with (optional) run-time type information as an exception. The overhead of having a strongly typed type system in C++ is noticed only at compile time, as more strict checks have to be performed on each variable assignment, operation, and conversion.
- Cortex-M3 + μC/OS-II嵌入式系統(tǒng)開發(fā)入門與應用
- Python GUI Programming:A Complete Reference Guide
- 計算機維修與維護技術(shù)速成
- VCD、DVD原理與維修
- Rapid BeagleBoard Prototyping with MATLAB and Simulink
- 計算機組裝維修與外設配置(高等職業(yè)院校教改示范教材·計算機系列)
- 面向?qū)ο蠓治雠c設計(第3版)(修訂版)
- 筆記本電腦應用技巧
- 單片機系統(tǒng)設計與開發(fā)教程
- RISC-V處理器與片上系統(tǒng)設計:基于FPGA與云平臺的實驗教程
- Arduino項目案例:游戲開發(fā)
- Blender for Video Production Quick Start Guide
- 計算機組成技術(shù)教程
- 分布式存儲系統(tǒng):核心技術(shù)、系統(tǒng)實現(xiàn)與Go項目實戰(zhàn)
- 3D打印:Geomagic Design X5.1 逆向建模設計實用教程