C++ 标准明确定义:
1.3.3 dynamic type [defns.dynamic.type]
the type of the most derived object (1.8) to which the lvalue denoted by an lvalue expression refers.
The dynamic type of an rvalue expression is its static type.
动态类型的定义是:由一个左值表达式指出的左值的动态类型,是其所引用的对象的最狭义类型。一个右值表达式的动态类型,就是它的静态类型。
1.3.11 static type [defns.static.type]
the type of an expression (3.9), which type results from analysis of the program without considering execution semantics. The static type of an expression depends only on the form of the program in which the expression appears, and does not change while the program is executing.
静态类型,是指不需要考虑表达式的执行期语义,仅从表达式的字面的形式就能够决定的类型。静态类型在程序运行时不会改变。
通常我们说,“基类指针指向的对象的实际/真正类型”或“基类引用所引用的对象的实际/真正类型”,就是它们的动态类型。很显然,这个动态类型是 C++ 语言通过指针和引用实现运行时多态能力的核心概念。
我将在考虑有关左值和右值的概念时继续对动态类型和静态类型的语义逻辑进行分析。
本文地址:http://com.8s8s.com/it/it26208.htm