您的位置:首页 > 编程语言 > C语言/C++

C++研究笔记(1)动态类型和静态类型

2004-10-27 17:02 239 查看
C++ 标准明确定义:

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.
静态类型,是指不需要考虑表达式的执行期语义,仅分析程序文本而决定的表达式类型。静态类型仅依赖于包含表达式的程序文本的形式,而在程序运行时不会改变,。

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. [Example: if a pointer (8.3.1)
p
whose static type is “pointer to class
B
” is pointing to an object of class
D
, derived from
B
(clause 10), the dynamic type of the expression
*p
is “
D
.” References (8.3.2) are treated similarly. ] The dynamic type of an rvalue expression is its static type.
动态类型是:由一个左值表达式表示的左值所引用的最终派生对象的类型。【例:如果一个静态类型为“类
B
的指针”的指针(8.3.1)
p
指向一个继承于
B
(章节10)的类
D
的对象,则表达式
*p
的动态类型为“
D
”。引用(8.3.2)按照相似规则处理。】一个右值表达式的动态类型,就是它的静态类型。

通常我们说,“基类指针指向的对象的实际/真正类型”或“基类引用所引用的对象的实际/真正类型”,就是它们的动态类型。很显然,这个动态类型是 C++ 语言通过指针和引用实现运行时多态能力的核心概念。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: