首页 技术 正文
技术 2022年11月14日
0 收藏 707 点赞 4,099 浏览 4129 个字
 1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 #include <typeinfo>
5
6 using std::cout;
7 class Grand
8 {
9 private:
10 int hold;
11 public:
12 Grand(int h=0):hold(h){}
13 virtual void Speak() const {cout << "I am a grand class!\n";}
14 virtual int Value() const {return hold;}
15 };
16
17 class Superb:public Grand
18 {
19 public:
20 Superb(int h=0):Grand(h){}
21 void Speak() const {cout << "I am a superb class!\n";}
22 virtual void Say() const
23 {
24 cout << "I hold the superb value of " << Value() << "!\n";
25 }
26 };
27
28 class Magnificent:public Superb
29 {
30 private:
31 char ch;
32 public:
33 Magnificent(int h=0, char c='A') : Superb(h),ch(c){}
34 void Speak() const {cout << "I am a magnificent class!!!\n";}
35 void Say() const {cout << "I hold the character " << ch << " and th e integer " << Value() << "!\n";}
36 };
37
38 Grand * Getone();
39
40 int main()
41 {
42 std::srand(std::time(0));
43 Grand * pg;
44 Superb * ps;
45 for(int i=0;i<5;i++)
46 {
47 pg=Getone();
48 cout << "Now processing type " << typeid(*pg).name() << ".\n";
49 pg->Speak();
50 if(ps=dynamic_cast<Superb *>(pg))
51 ps->Say();
52 if(typeid(Magnificent)==typeid(*pg))
53 cout << "Yes, you're really magnificent.\n";
54 }
55 return 0;
56 }
57
58 Grand * Getone()
59 {
60 Grand * p;
61 switch(std::rand()%3)
62 {
63 case 0: p=new Grand(std::rand()%100);
64 break;
65 case 1:p=new Superb(std::rand()%100);
66 break;
67 case 2:p=new Magnificent(std::rand()%100,'A'+std::rand()%26);
68 break;
69 }
70 return p;
71 }
 1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 #include <typeinfo>
5
6 using std::cout;
7 class Grand
8 {
9 private:
10 int hold;
11 public:
12 Grand(int h=0):hold(h){}
13 virtual void Speak() const {cout << "I am a grand class!\n";}
14 virtual int Value() const {return hold;}
15 };
16
17 class Superb:public Grand
18 {
19 public:
20 Superb(int h=0):Grand(h){}
21 void Speak() const {cout << "I am a superb class!\n";}
22 virtual void Say() const
23 {
24 cout << "I hold the superb value of " << Value() << "!\n";
25 }
26 };
27
28 class Magnificent:public Superb
29 {
30 private:
31 char ch;
32 public:
33 Magnificent(int h=0, char c='A') : Superb(h),ch(c){}
34 void Speak() const {cout << "I am a magnificent class!!!\n";}
35 void Say() const {cout << "I hold the character " << ch << " and th e integer " << Value() << "!\n";}
36 };
37
38 Grand * Getone();
39
40 int main()
41 {
42 std::srand(std::time(0));
43 Grand * pg;
44 Superb * ps;
45 for(int i=0;i<5;i++)
46 {
47 pg=Getone();
48 cout << "Now processing type " << typeid(*pg).name() << ".\n";
49 pg->Speak();
50 if(ps=dynamic_cast<Superb *>(pg))
51 ps->Say();
52 if(typeid(Magnificent)==typeid(*pg))
53 cout << "Yes, you're really magnificent.\n";
54 }
55 return 0;
56 }
57
58 Grand * Getone()
59 {
60 Grand * p;
61 switch(std::rand()%3)
62 {
63 case 0: p=new Grand(std::rand()%100);
64 break;
65 case 1:p=new Superb(std::rand()%100);
66 break;
67 case 2:p=new Magnificent(std::rand()%100,'A'+std::rand()%26);
68 break;
69 }
70 return p;
71 }
 1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 #include <typeinfo>
5
6 using std::cout;
7 class Grand
8 {
9 private:
10 int hold;
11 public:
12 Grand(int h=0):hold(h){}
13 virtual void Speak() const {cout << "I am a grand class!\n";}
14 virtual int Value() const {return hold;}
15 };
16
17 class Superb:public Grand
18 {
19 public:
20 Superb(int h=0):Grand(h){}
21 void Speak() const {cout << "I am a superb class!\n";}
22 virtual void Say() const
23 {
24 cout << "I hold the superb value of " << Value() << "!\n";
25 }
26 };
27
28 class Magnificent:public Superb
29 {
30 private:
31 char ch;
32 public:
33 Magnificent(int h=0, char c='A') : Superb(h),ch(c){}
34 void Speak() const {cout << "I am a magnificent class!!!\n";}
35 void Say() const {cout << "I hold the character " << ch << " and th e integer " << Value() << "!\n";}
36 };
37
38 Grand * Getone();
39
40 int main()
41 {
42 std::srand(std::time(0));
43 Grand * pg;
44 Superb * ps;
45 for(int i=0;i<5;i++)
46 {
47 pg=Getone();
48 cout << "Now processing type " << typeid(*pg).name() << ".\n";
49 pg->Speak();
50 if(ps=dynamic_cast<Superb *>(pg))
51 ps->Say();
52 if(typeid(Magnificent)==typeid(*pg))
53 cout << "Yes, you're really magnificent.\n";
54 }
55 return 0;
56 }
57
58 Grand * Getone()
59 {
60 Grand * p;
61 switch(std::rand()%3)
62 {
63 case 0: p=new Grand(std::rand()%100);
64 break;
65 case 1:p=new Superb(std::rand()%100);
66 break;
67 case 2:p=new Magnificent(std::rand()%100,'A'+std::rand()%26);
68 break;
69 }
70 return p;
71 }

typeid运算符能够确定两个对象是否为同种类型。它与sizeof有些相像,可以接受两种参数:

类型;

结果为对象的表达式。

typeid运算符返回一个对type_info对象的引用,其中,type_info是在头文件typeinfo中定义的一个类。type_info类重在了==和!==运算符,以便可以使用这些运算符来对类型进行比较。例如,如果pg指向的是一个Magnificent对象,则下述表达式返回true,否则为false:

typeid(Magnificent)==typeid(*pg)

如果pg是一个空指针,程序将引发bad_typeid异常。

type_info类的实现随厂商而异,但包含一个name()成员,该函数返回一个随实现而异的字符串,通常为类的名称。本程序返回的是类的名称长度+类的名称。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781