C ++是静态类型语言,为什么我们可以在运行时获取类型 [英] C++ is static typed language, why can we get type at runtime

查看:122
本文介绍了C ++是静态类型语言,为什么我们可以在运行时获取类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  TYPE& dynamic_cast< TYPE&> (目的); 
TYPE * dynamic_cast< TYPE *> (目的);

例如,我们可以得到类似这样的类型。
C ++是静态类型语言,为什么我们可以在运行时获取类型

解决方案

在C ++中有一个静态确定的类型。 不一定。



例如:

pre> int * p = new int [argc]; //有一个动态类型为int [argc]的对象

Base * p = new Derived; // * p具有动态类型Derived

Base * q = rand()%2? new Derived1:new Derived2; // ???


TYPE& dynamic_cast<TYPE&> (object);
TYPE* dynamic_cast<TYPE*> (object);

For example we can get type like this. C++ is static typed language, why can we get type at runtime

解决方案

Variables in C++ have a statically determined type. Objects don't necessarily. You only need to handle objects through variables of statically known type.

Examples:

int * p = new int[argc];   // there is an object whose dynamic type is int[argc]

Base * p = new Derived;    // *p has dynamic type Derived

Base * q = rand() % 2 ? new Derived1 : new Derived2;    // ???

这篇关于C ++是静态类型语言,为什么我们可以在运行时获取类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆