对象的静态类型与动态类型

由于继承导致对象的指针和引用具有两种不同的类型: 静态类型 和 动态类型 。
静态类型 :指针或者是引用声明时的类型。
动态类型 :由他实际指向的类型确定。
例如:

GameObject *pgo = new SpaceShip; //pgo静态类型是 GameObject动态类型是 SpaceShip*
Asterioid *pa = new Asterioid; //pa的静态类型是 Asterioid *
//动态类型也是 Asterioid *
pgo = pa; //pgo静态类型总指向 GameObject *
//动态类型指向了 Asterioid *
GameObject &rgo = *pa; //rgo的静态类型是 GameObject
//动态类型是 Asterioi
  • 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.
    静态类型,是指不需要考虑表达式的执行期语义,仅分析程序文本而决定的表达式类型。静态类型仅依赖于包含表达式的程序文本的形式,而在程序运行时不会改变,。
  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值