C++的问题,请进

来源:百度知道 编辑:UC知道 时间:2024/06/02 08:13:33
我看过一本C++教材上说:
静态类型:编译时期的类型,就是编译器看到的类型,
动态类型:运行时期的类型,这个编译器无发知道,只有程序运行时刻才能确定
这样说对吗?我不理解,请帮我讲一下吧,谢谢!
动态类型不是new分配的类型吗?

C++ 标准明确定义:
dynamic type [defns.dynamic.type]
the type of the most derived object to which the lvalue denoted by an lvalue expression refers.
The dynamic type of an rvalue expression is its static type.
动态类型的定义是:由一个左值表达式指出的左值的动态类型,是其所引用的对象的最狭义类型。一个右值表达式的动态类型,就是它的静态类型。

static type [defns.static.type]
the type of an expression , 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++ 语言通过指针和引用实现运行时多态能力的核心概念。

new分配的不是运行时刻才能确定的吗?

静态和动态应该是分配数据类型空间的时候所处时间不一样,一个是程序一开始就固定了的,一个是执行期间按程序结构来及时分配的