栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

详解c++中的类型识别

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解c++中的类型识别

1、类型识别的相关概念

(1)类型识别的作用

  类型识别是面向对象中引入的一个新概念,主要用来判断赋值兼容性原则中的类型问题,即此时的数据类型到底是基类类型还是派生类类型?

  当基类指针指向子类对象 或者基类引用成为子类对象的别名 时,就需要使用类型识别;

base *p = new Derived();
base &r = *p

对于上面的语句,我们可以这样认识,指针p是base类型,但是P 又指向了一个新的Derived类型,此时很难判断指针P 的数据类型;同理,引用r 本来作为父类的别名而存在,但由于赋值兼容性,引用r也可以作为子类的别名,同样此时 引用 r 的数据类型也不能确定;

  注:1)由之前所学知识,若没有虚函数重写,编译器为了安全起见,会将指针p 当作 base 类型;(编译期间)    

    2)若有虚函数重写,就会发生动态多态特性,此时就会根据指针p 所指向的具体数据类型来确定指针p 的数据类型。(运行期间)

(2)类型识别的分类

  1)静态类型:变量(对象)自身的类型;在编译阶段就能确定所使用变量的数据类型。

  2)动态类型:指针(引用)所指向对象的实际类型;在运行阶段根据指针所指向的具体数据类型来确定所使用的数据类型。

    base *b 所指向的实际对象无法确定,若指针b 指向的是子类对象,则程序正常运行;若指针b 指向的是父类对象,则程序有可能出现 Bug;

    注:在 g++ 编译器下上述情况均可正常运行,但后者不建议使用;

  在赋值兼容原则中,基类指针是否可以强制类型转换为子类指针取决于动态类型;(很重要!!!)--- 只有动态类型是子类对象才能进行合法转换

2、如何得到动态类型

(1)利用多态

  1)必须从基类开始提供类型虚函数;

  2)所有的派生类都必须重写类型虚函数;

  3)每个派生类的类型 ID必须唯一;

   结果:调用类型虚函数就可以知道当前的对象究竟是什么类型,这样就可以得到动态类型,达到动态类型识别效果;

利用类型虚函数实现类型识别

 #include 
 #include 
 
 using namespace std;
 
 class base
 {
 public:
   enum { ID = 0 };
   
   virtual int type() // 类型虚函数
   {
     return ID;
   }
 };
 
 class Derived : public base
 {
 public:
   enum { ID = 1 };
   
   int type()
   {
     return ID;
   }
   
   void print()
   {
     cout << "I'm a Derived. " << endl;
   }
 };
 
 class Child : public base
 {
 public:
   enum { ID = 2 };
   
   int type()
   {
     return ID;
   }
 };
 
 void test(base* pb)
 {
   if( pb->type() == Child::ID )
   {
     Child* pc = static_cast(pb);
     //Child* pc = dynamic_cast(pb);  // 同上
     
     cout << "& = " << pc << endl;
     cout << "I'm a Child. " << endl;
   }
   
   if( pb->type() == Derived::ID )
   {
     Derived* pd = static_cast(pb);
     //Derived* pd = dynamic_cast(pb); // 同上
     
     cout << "& = " << pd << endl;
     pd->print();
   }
   
   if( pb->type() == base::ID )
   {
     cout << "& = " << pb << endl;
     cout << "I'm a base. " << endl;
   }
 }
 
 int main(int argc, char *argv[])
 {
   base b;
   Derived d;
   Child c;
   
   test(&b);
   test(&d);
   test(&c);
   
   return 0;
 }
 

(2)利用 dynamic_cast

  1)dynamic_cast这个关键字如果要转换的实际类型和指定的类型不一样,则会返回NULL。例如当指定类型为子类对象时,如果父类指针的动态类型是这个子类对象时,转换成功,而动态类型是父类对象或者其他子类对象时,转换失败;

  2)dynamic_cast 要求使用的目标对象类型必须是多态,即:所在类族至少有一个虚函数;

  3)只能用于指针和引用之间的转换

    1.用于指针转换时,转换失败,返回空指针;

    2.用于引用转换时,转换失败,将引发 bad_cast异常。

 #include 
 #include 
 
 using namespace std;
 
 class base
 {
 public:
   virtual ~base()
   {
   
   }
 };
 
 class Derived : public base
 {
 public:  
   void print()
   {
     cout << "I'm a Derived. " << endl;
   }
 };
 
 class Child : public base
 {
 
 };
 
 void test(base* pb)
 {
   // dynamic_cast 只能确定最终的转化结果,无法获取动态类型的原型
   Derived* pd = dynamic_cast(pb);
   
   if(pd != NULL)
   {
     // Derived 类类型, 可以使用指针pd访问Derived类的成员
     cout << "& = " << pd << endl;
     pd->print();
   }
   else
   {
     Child* pc = dynamic_cast(pb);
     
     if(pc != NULL)
     {
// Child 类类型, 可以使用指针pc访问Child类的成员
cout << "& = " << pc << endl;
cout << "I'm a Child. " << endl;
     }
     else
     {
// base 类类型, 可以使用指针pb访问base类的成员
cout << "& = " << pc << endl; 
cout << "I'm a base. " << endl;
     }
   }
 }
 
 int main(int argc, char *argv[])
 {
   base b;
   Derived d;
   Child c;
   
   test(&b);
   test(&d);
   test(&c);
   
   return 0;
 }
 

(3)利用 typeid(推荐这种方法)

  1)typeid是一个关键字,专门用于动态类型识别;

  2)typeid 关键字返回对应参数的类型信息,此类型信息是一个type_info类对象;

    1.当参数为类型时,返回静态类型信息;

    2.当参数为变量时:1> 参数变量内部不存在虚函数表时,返回静态类型信息; 2> 参数变量内部存在虚函数表时,返回动态类型信息;

    3.当参数为 NULL 时,将抛出异常;

  3)typeid使用时需要包含头文件

  4)typeid 使用时直接指定对象或者类型。

  5)typeid 在不同的编译器内部实现是不同的;

int i = 0;

const type_info& tiv = typeid(i); // 将 i 的类型信息放到 type_info 中去;
const type_info& tii = typeid(int);

cout << (tiv == tii) << endl; // 1

利用 typeid 实现类型识别

 #include 
  #include 
  #include 
  
  using namespace std;
  
  class base
  {
  public:
   virtual ~base()
   {
   }
 };
  
 class Derived : public base
 {
 public:
   void print()
   {
     cout << "I'm a Derived." << endl;
   }
 };
  
 class Child : public base 
 {
 public:
   void print()
   {
     cout << "I'm a Child." << endl;
   }
 };
  
 void test(base* pb)
 {
   const type_info& tb = typeid(*pb);
   
   if( tb == typeid(Derived) )
   {
     Derived* pd = dynamic_cast(pb);
   
     cout << "& = " << pd << endl;
     pd->print();
   }
   else if( tb == typeid(Child) )
   {
     Child* pc = dynamic_cast(pb);
     
     cout << "& = " << pc << endl;
     pc->print();
     
   }
   else if( tb == typeid(base) )
   {
     cout << "& = " << pb << endl;
     cout << "I'm a base. " << endl;
   }
   
   cout << tb.name() << endl;
 }
  
 int main(int argc, char *argv[])
 {
   base b;
   Derived d;
   Child c;
   int index;
   char ch;
   
   const type_info& tp = typeid(b);
   const type_info& tc = typeid(d);
   const type_info& tn = typeid(c);
   const type_info& ti = typeid(index);
   const type_info& tch = typeid(ch);
   
   cout<

  结论:

  3 种动态类型的实现方法 建议选 第3种 (typeid)。

  对于多态实现,存在以下缺陷:

    1)必须从基类开始提供类型虚函数;

  2)所有的派生类都必须重写类型虚函数;

  3)每个派生类的类型名必须唯一;

  对于 dynamic_cast 实现,只能得到类型转换的结果,不能获取真正的动态类型,同时dynamic_cast 必须多态实现。

到此这篇关于 详解c++中的类型识别的文章就介绍到这了,更多相关c++ 类型识别内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61546.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号