在 C++ / value category 语境里,这几个词常用英文是:
| 中文 | 英语 |
|—|—|
| 动态类型 |dynamic type|
| 静态类型 |static type|
| 多态 |polymorphism|
| 多态的 |polymorphic|
| 多态类型 |polymorphic type|
| glvalue |glvalue,全称generalized lvalue|
例句
1. 动态类型 / 静态类型
Base*p=newDerived;可以说:
The static type of*pisBase, while its dynamic type isDerived.
中文:
*p的静态类型是Base,而它的动态类型是Derived。
2. 多态
structBase{virtualvoidf();};structDerived:Base{voidf()override;};可以说:
Baseis a polymorphic type because it has a virtual function.
中文:
Base是一个多态类型,因为它有虚函数。
C++ 标准语境里的表达
在讨论 glvalue 时,经常会看到类似说法:
A glvalue has an identity and may have a dynamic type.
中文大意:
glvalue 有身份标识,并且可能具有动态类型。
再比如:
If a glvalue refers to a polymorphic object, its dynamic type may differ from its static type.
中文:
如果一个 glvalue 指代一个多态对象,它的动态类型可能不同于它的静态类型。
简单理解
static type:编译器在编译期看到的类型
dynamic type:程序运行时对象真正的类型
polymorphism:通过基类指针/引用调用派生类行为的机制
polymorphic type:有至少一个虚函数的类类型
例如:
structBase{virtual~Base()=default;};structDerived:Base{};Base&ref=*(newDerived);这里:
ref的static type是Base&ref绑定对象的dynamic type是DerivedBase是polymorphic type
一句话记忆:
static type = 编译器看到的类型;
dynamic type = 运行时真实对象类型;
polymorphism = 多态。