1 #include "mainwindow.h"
2 #include <QApplication>
3 #include <list>
4 #include <QPushButton>
5 #include<QLabel>
6 #include<QLineEdit>
7
8 using namespace std;
9
10 //异构数据结构,每个数组元素都是基类指针
11 //异构链表,每一个结点都是基类的指针,指向了派生类
12 //基类指针基于多态与虚函数调用派生类的方法
13
14 class object
15 {
16 public:
17 virtual void showit()
18 {
19
20 }
21 };
22
23 class mywindow:public object
24 {
25 public:
26 MainWindow *p;
27
28 mywindow()
29 {
30 p = new MainWindow;
31 }
32
33 void showit()
34 {
35 p->show();
36 }
37 };
38
39 class mylabel:public object
40 {
41 public:
42 QLabel *p;
43 mylabel()
44 {
45 p=new QLabel;
46 }
47 void showit()
48 {
49 p->show();
50 }
51 };
52
53 class myedit:public object
54 {
55 public:
56 QLineEdit *p;
57 myedit()
58 {
59 p=new QLineEdit;
60 }
61 void showit()
62 {
63 p->show();
64 }
65 };
66
67 class mybutton:public object
68 {
69 public:
70 QPushButton *p;
71 mybutton()
72 {
73 p = new QPushButton;
74 }
75
76 void showit()
77 {
78 p->show();
79 }
80 };
81
82 int main(int argc, char *argv[])
83 {
84 QApplication a(argc, argv);
85
86 list<object *>mylist;
87
88 object *p = new mywindow;
89 mylist.push_back(p);
90
91 p = new mylabel;
92 mylist.push_back(p);
93
94 p = new mybutton;
95 mylist.push_back(p);
96
97 p = new mybutton;
98 mylist.push_back(p);
99
100 for(auto it:mylist)
101 {
102 (*it).showit();
103 }
104 return a.exec();
105 }