C++ typeid: find out type in rea…

来源:互联网 发布:失落沙洲知乎 编辑:程序博客网 时间:2024/06/06 04:16

  typeid用法

 http://blog.csdn.net/haozhi_hit/archive/2006/11/17/1392960.aspx

#include <iostream>
#include <typeinfo>
using namespace std;
class Test
{
public:
 virtual ~Test()
 {
 }
 virtual void Out()
 {
  cout<<typeid(*this).name()<<endl;
 }
 Test()
 {
  cout<<typeid(*this).name()<<endl;
 }
};
class FromTest : public Test
{
public:
 FromTest(){}
};
int main()
{
// void * v=new Test;
//    Test *t=static_cast<Test *>(v);
//// Test * t=(Test *)(v);
// cout<<typeid(t).name()<<endl;
// t->Out();
// int i;
// cout<<typeid(i).name()<<endl;
 FromTest ft;

 Test * t;//=new FromTest;
 Test l;
 l.Out(); 
 t=&ft;
 cout<<typeid(*t).name()<<endl;
}


Results:

 

$ ./typeid.exe

4Test

4Test

4Test

8FromTest


 

0 0
原创粉丝点击