类继承

 
 1 #include <stdio.h>                                                                                                 
 2 
 3 class CSofa
 4 {
 5     public: 
 6         CSofa() 
 7         {       
 8             m_nColor = 2;
 9         }       
10        
11         virtual ~CSofa()
12         {       
13             printf("virtual ~CSofa() \r\n");
14         }       
15 
16         virtual int GetColor()
17         {       
18             return m_nColor;
19         }       
20 
21         virtual int SitDown()
22         {       
23             return printf("Sit down and rest your legs \r\n");
24         }       
25 
26     protected:
27         int m_nColor;
28 };
29 
30 class CBed
31 {
32     public: 
33         CBed()  
34         {       
35             m_nLength = 4;
36             m_nWidth = 5;
37         }       
38 
39         virtual ~CBed() 
40         {
41             printf("virtual ~CBed() \r\n");
42         }
43 
44         virtual int GetArea()
45         {
46             return m_nLength * m_nWidth;
47         }
48 
49         virtual int Sleep()
50         {
51             return printf("go to sleep \r\n");
52         }
53 
54     protected:
55         int m_nLength;
56         int m_nWidth;
57 };
58 
59 class CSofaBed : public CSofa, public CBed
60 {
61     public:
62         CSofaBed()
63         {
64             m_nHeight = 6;
65         }
66 
67         virtual ~CSofaBed()
68         {
69             printf("virtual ~CSofaBed() \r\n");
70         }
71 
72         virtual int SitDown()
73         {
74             return printf("Sit down on the sofa bed \r\n");
75         }
76 
77         virtual int Sleep()
78         {
79             return printf("go to sleep on the sofa bed \r\n");    
80         }
81 
82         virtual int GetHeight()
83         {
84             return m_nHeight;
85         }
86 
87     protected:
88         int m_nHeight;
89 };
90 
91 
92 int main(void)
93 {
94 
95     CSofaBed first;
96     return 0;
97 }             

重点关注下构造函数,析构函数的调用,虚表指针的赋值,虚表的构成

  1 08048f40 <main>:                                                                                                   
  2  8048f40:   55                      push   %ebp
  3  8048f41:   89 e5                   mov    %esp,%ebp
  4  8048f43:   53                      push   %ebx
  5  8048f44:   83 e4 f0                and    $0xfffffff0,%esp
  6  8048f47:   83 ec 30                sub    $0x30,%esp
  7  8048f4a:   8d 44 24 18             lea    0x18(%esp),%eax
  8  8048f4e:   89 04 24                mov    %eax,(%esp)
  9  8048f51:   e8 3e 01 00 00          call   8049094 <_ZN8CSofaBedC1Ev> CSofaBed::CSofaBed()
 10  8048f56:   bb 00 00 00 00          mov    $0x0,%ebx
 11  8048f5b:   8d 44 24 18             lea    0x18(%esp),%eax
 12  8048f5f:   89 04 24                mov    %eax,(%esp)
 13  8048f62:   e8 75 01 00 00          call   80490dc <_ZN8CSofaBedD1Ev> CSofaBed::~CSofaBed()
 14  8048f67:   89 d8                   mov    %ebx,%eax
 15  8048f69:   8b 5d fc                mov    -0x4(%ebp),%ebx
 16  8048f6c:   c9                      leave   
 17  8048f6d:   c3                      ret     
 18 
 19 08048f6e <_ZN5CSofaC1Ev>: CSofa::CSofa()
 20  8048f6e:   55                      push   %ebp
 21  8048f6f:   89 e5                   mov    %esp,%ebp
 22  8048f71:   8b 45 08                mov    0x8(%ebp),%eax
 23  8048f74:   c7 00 18 f9 0c 08       movl   $0x80cf918,(%eax) vtable for CSofa
 24  8048f7a:   8b 45 08                mov    0x8(%ebp),%eax
 25  8048f7d:   c7 40 04 02 00 00 00    movl   $0x2,0x4(%eax)
 26  8048f84:   5d                      pop    %ebp    
 27  8048f85:   c3                      ret     
 28 
 29 08048f86 <_ZN5CSofaD1Ev>: CSofa::~CSofa()
 30  8048f86:   55                      push   %ebp
 31  8048f87:   89 e5                   mov    %esp,%ebp
 32  8048f89:   83 ec 18                sub    $0x18,%esp
 33  8048f8c:   8b 45 08                mov    0x8(%ebp),%eax
 34  8048f8f:   c7 00 18 f9 0c 08       movl   $0x80cf918,(%eax)
 35  8048f95:   c7 04 24 08 f8 0c 08    movl   $0x80cf808,(%esp)
 36  8048f9c:   e8 af ef 00 00          call   8057f50 <_IO_puts>
 37  8048fa1:   b8 00 00 00 00          mov    $0x0,%eax
 38  8048fa6:   83 e0 01                and    $0x1,%eax
 39  8048fa9:   84 c0                   test   %al,%al 
 40  8048fab:   74 0b                   je     8048fb8 <_ZN5CSofaD1Ev+0x32>
 41  8048fad:   8b 45 08                mov    0x8(%ebp),%eax
 42  8048fb0:   89 04 24                mov    %eax,(%esp)
 43  8048fb3:   e8 f8 07 00 00          call   80497b0 <_ZdlPv>
 44  8048fb8:   c9                      leave  
 45  8048fb9:   c3                      ret    
 46 
 47 08048fba <_ZN5CSofaD0Ev>: CSofa::~CSofa()
 48  8048fba:   55                      push   %ebp
 49  8048fbb:   89 e5                   mov    %esp,%ebp
 50  8048fbd:   83 ec 18                sub    $0x18,%esp
 51  8048fc0:   8b 45 08                mov    0x8(%ebp),%eax
 52  8048fc3:   89 04 24                mov    %eax,(%esp)
 53  8048fc6:   e8 bb ff ff ff          call   8048f86 <_ZN5CSofaD1Ev>
 54  8048fcb:   8b 45 08                mov    0x8(%ebp),%eax
 55  8048fce:   89 04 24                mov    %eax,(%esp)
 56  8048fd1:   e8 da 07 00 00          call   80497b0 <_ZdlPv>
 57  8048fd6:   c9                      leave  
 58  8048fd7:   c3                      ret    
 59 
 60 08048fd8 <_ZN5CSofa8GetColorEv>: CSofa::GetColor()
 61  8048fd8:   55                      push   %ebp
 62  8048fd9:   89 e5                   mov    %esp,%ebp
 63  8048fdb:   8b 45 08                mov    0x8(%ebp),%eax
 64  8048fde:   8b 40 04                mov    0x4(%eax),%eax
 65  8048fe1:   5d                      pop    %ebp
 66  8048fe2:   c3                      ret    
 67  8048fe3:   90                      nop
 68 
 69 08048fe4 <_ZN5CSofa7SitDownEv>: CSofa::SitDown()
 70  8048fe4:   55                      push   %ebp
 71  8048fe5:   89 e5                   mov    %esp,%ebp
 72  8048fe7:   83 ec 18                sub    $0x18,%esp
 73  8048fea:   c7 04 24 1c f8 0c 08    movl   $0x80cf81c,(%esp)
 74  8048ff1:   e8 8a ec 00 00          call   8057c80 <_IO_printf>
 75  8048ff6:   c9                      leave  
 76  8048ff7:   c3                      ret    
 77 
 78 08048ff8 <_ZN4CBedC1Ev>: CBed::CBed()
 79  8048ff8:   55                      push   %ebp
 80  8048ff9:   89 e5                   mov    %esp,%ebp 
 81  8048ffb:   8b 45 08                mov    0x8(%ebp),%eax
 82  8048ffe:   c7 00 00 f9 0c 08       movl   $0x80cf900,(%eax) vtable for CBed
 83  8049004:   8b 45 08                mov    0x8(%ebp),%eax
 84  8049007:   c7 40 04 04 00 00 00    movl   $0x4,0x4(%eax)
 85  804900e:   8b 45 08                mov    0x8(%ebp),%eax
 86  8049011:   c7 40 08 05 00 00 00    movl   $0x5,0x8(%eax)
 87  8049018:   5d                      pop    %ebp
 88  8049019:   c3                      ret    
 89 
 90 0804901a <_ZN4CBedD1Ev>: CBed::~CBed()
 91  804901a:   55                      push   %ebp
 92  804901b:   89 e5                   mov    %esp,%ebp
 93  804901d:   83 ec 18                sub    $0x18,%esp
 94  8049020:   8b 45 08                mov    0x8(%ebp),%eax
 95  8049023:   c7 00 00 f9 0c 08       movl   $0x80cf900,(%eax)
 96  8049029:   c7 04 24 3b f8 0c 08    movl   $0x80cf83b,(%esp)
 97  8049030:   e8 1b ef 00 00          call   8057f50 <_IO_puts>
 98  8049035:   b8 00 00 00 00          mov    $0x0,%eax
 99  804903a:   83 e0 01                and    $0x1,%eax
100  804903d:   84 c0                   test   %al,%al
101  804903f:   74 0b                   je     804904c <_ZN4CBedD1Ev+0x32> CBed::~CBed() + 0x32
102  8049041:   8b 45 08                mov    0x8(%ebp),%eax
103  8049044:   89 04 24                mov    %eax,(%esp)
104  8049047:   e8 64 07 00 00          call   80497b0 <_ZdlPv>
105  804904c:   c9                      leave  
106  804904d:   c3                      ret    
107 
108 0804904e <_ZN4CBedD0Ev>: CBed::~CBed()
109  804904e:   55                      push   %ebp
110  804904f:   89 e5                   mov    %esp,%ebp
111  8049051:   83 ec 18                sub    $0x18,%esp
112  8049054:   8b 45 08                mov    0x8(%ebp),%eax
113  8049057:   89 04 24                mov    %eax,(%esp)
114  804905a:   e8 bb ff ff ff          call   804901a <_ZN4CBedD1Ev>
115  804905f:   8b 45 08                mov    0x8(%ebp),%eax
116  8049062:   89 04 24                mov    %eax,(%esp)
117  8049065:   e8 46 07 00 00          call   80497b0 <_ZdlPv>
118  804906a:   c9                      leave  
119  804906b:   c3                      ret    
120                                                  
121 0804906c <_ZN4CBed7GetAreaEv>: CBed::GetArea()
122  804906c:   55                      push   %ebp
123  804906d:   89 e5                   mov    %esp,%ebp
124  804906f:   8b 45 08                mov    0x8(%ebp),%eax
125  8049072:   8b 50 04                mov    0x4(%eax),%edx
126  8049075:   8b 45 08                mov    0x8(%ebp),%eax
127  8049078:   8b 40 08                mov    0x8(%eax),%eax
128  804907b:   0f af c2                imul   %edx,%eax
129  804907e:   5d                      pop    %ebp
130  804907f:   c3                      ret    
131 
132 08049080 <_ZN4CBed5SleepEv>: CBed::Sleep()
133  8049080:   55                      push   %ebp
134  8049081:   89 e5                   mov    %esp,%ebp
135  8049083:   83 ec 18                sub    $0x18,%esp
136  8049086:   c7 04 24 4d f8 0c 08    movl   $0x80cf84d,(%esp)
137  804908d:   e8 ee eb 00 00          call   8057c80 <_IO_printf>
138  8049092:   c9                      leave  
139  8049093:   c3                      ret    
140 
141 08049094 <_ZN8CSofaBedC1Ev>: CSofaBed::CSofaBed()
142  8049094:   55                      push   %ebp
143  8049095:   89 e5                   mov    %esp,%ebp
144  8049097:   83 ec 18                sub    $0x18,%esp
145  804909a:   8b 45 08                mov    0x8(%ebp),%eax
146  804909d:   89 04 24                mov    %eax,(%esp)
147  80490a0:   e8 c9 fe ff ff          call   8048f6e <_ZN5CSofaC1Ev> CSofa::CSofa()
148  80490a5:   8b 45 08                mov    0x8(%ebp),%eax
149  80490a8:   83 c0 08                add    $0x8,%eax
150  80490ab:   89 04 24                mov    %eax,(%esp)
151  80490ae:   e8 45 ff ff ff          call   8048ff8 <_ZN4CBedC1Ev> CBed::CBed()
152  80490b3:   8b 45 08                mov    0x8(%ebp),%eax
153  80490b6:   c7 00 c8 f8 0c 08       movl   $0x80cf8c8,(%eax) vtable for CSofaBed
154  80490bc:   8b 45 08                mov    0x8(%ebp),%eax
155  80490bf:   c7 40 08 e8 f8 0c 08    movl   $0x80cf8e8,0x8(%eax)
156  80490c6:   8b 45 08                mov    0x8(%ebp),%eax
157  80490c9:   c7 40 14 06 00 00 00    movl   $0x6,0x14(%eax)
158  80490d0:   c9                      leave  
159  80490d1:   c3                      ret    
160                                       
161 080490d2 <_ZThn8_N8CSofaBedD1Ev>: non-virtual thunk to CSofaBed::~CSofaBed()
162  80490d2:   83 6c 24 04 08          subl   $0x8,0x4(%esp)
163  80490d7:   e9 00 00 00 00          jmp    80490dc <_ZN8CSofaBedD1Ev>
164 
165 080490dc <_ZN8CSofaBedD1Ev>: CSofaBed::~CSofaBed()
166  80490dc:   55                      push   %ebp
167  80490dd:   89 e5                   mov    %esp,%ebp
168  80490df:   53                      push   %ebx
169  80490e0:   83 ec 14                sub    $0x14,%esp
170  80490e3:   8b 45 08                mov    0x8(%ebp),%eax
171  80490e6:   c7 00 c8 f8 0c 08       movl   $0x80cf8c8,(%eax)
172  80490ec:   8b 45 08                mov    0x8(%ebp),%eax
173  80490ef:   c7 40 08 e8 f8 0c 08    movl   $0x80cf8e8,0x8(%eax)
174  80490f6:   c7 04 24 5c f8 0c 08    movl   $0x80cf85c,(%esp)
175  80490fd:   e8 4e ee 00 00          call   8057f50 <_IO_puts>
176  8049102:   8b 45 08                mov    0x8(%ebp),%eax
177  8049105:   83 c0 08                add    $0x8,%eax
178  8049108:   89 04 24                mov    %eax,(%esp)
179  804910b:   e8 0a ff ff ff          call   804901a <_ZN4CBedD1Ev> CBed::~CBed()
180  8049110:   8b 45 08                mov    0x8(%ebp),%eax
181  8049113:   89 04 24                mov    %eax,(%esp)
182  8049116:   e8 6b fe ff ff          call   8048f86 <_ZN5CSofaD1Ev>
183  804911b:   b8 00 00 00 00          mov    $0x0,%eax
184  8049120:   83 e0 01                and    $0x1,%eax
185  8049123:   84 c0                   test   %al,%al
186  8049125:   74 36                   je     804915d <_ZN8CSofaBedD1Ev+0x81>
187  8049127:   8b 45 08                mov    0x8(%ebp),%eax
188  804912a:   89 04 24                mov    %eax,(%esp)
189  804912d:   e8 7e 06 00 00          call   80497b0 <_ZdlPv>
190  8049132:   eb 29                   jmp    804915d <_ZN8CSofaBedD1Ev+0x81>
191  8049134:   89 c3                   mov    %eax,%ebx
192  8049136:   8b 45 08                mov    0x8(%ebp),%eax
193  8049139:   83 c0 08                add    $0x8,%eax
194  804913c:   89 04 24                mov    %eax,(%esp)
195  804913f:   e8 d6 fe ff ff          call   804901a <_ZN4CBedD1Ev>
196  8049144:   eb 02                   jmp    8049148 <_ZN8CSofaBedD1Ev+0x6c> CSofaBed::~CSofaBed()
197  8049146:   89 c3                   mov    %eax,%ebx
198  8049148:   8b 45 08                mov    0x8(%ebp),%eax
199  804914b:   89 04 24                mov    %eax,(%esp)
200  804914e:   e8 33 fe ff ff          call   8048f86 <_ZN5CSofaD1Ev> CSofa::~CSofa()
201 
202  8049153:   89 d8                   mov    %ebx,%eax
203  8049155:   89 04 24                mov    %eax,(%esp)
204  8049158:   e8 b3 bf 00 00          call   8055110 <_Unwind_Resume>
205  804915d:   83 c4 14                add    $0x14,%esp
206  8049160:   5b                      pop    %ebx
207  8049161:   5d                      pop    %ebp
208  8049162:   c3                      ret    
209  8049163:   90                      nop
210 
211 08049164 <_ZThn8_N8CSofaBedD0Ev>: non-virtual thunk to CSofaBed::~CSofaBed()
212  8049164:   83 6c 24 04 08          subl   $0x8,0x4(%esp)
213  8049169:   eb 01                   jmp    804916c <_ZN8CSofaBedD0Ev>
214  804916b:   90                      nop
215 
216 0804916c <_ZN8CSofaBedD0Ev>: CSofaBed::~CSofaBed()
217  804916c:   55                      push   %ebp
218  804916d:   89 e5                   mov    %esp,%ebp
219  804916f:   83 ec 18                sub    $0x18,%esp
220  8049172:   8b 45 08                mov    0x8(%ebp),%eax
221  8049175:   89 04 24                mov    %eax,(%esp)
222  8049178:   e8 5f ff ff ff          call   80490dc <_ZN8CSofaBedD1Ev>
223  804917d:   8b 45 08                mov    0x8(%ebp),%eax
224  8049180:   89 04 24                mov    %eax,(%esp)
225  8049183:   e8 28 06 00 00          call   80497b0 <_ZdlPv>
226  8049188:   c9                      leave  
227  8049189:   c3                      ret    
228 
229 0804918a <_ZN8CSofaBed7SitDownEv>: CSofaBed::SitDown()
230  804918a:   55                      push   %ebp
231  804918b:   89 e5                   mov    %esp,%ebp
232  804918d:   83 ec 18                sub    $0x18,%esp
233  8049190:   c7 04 24 72 f8 0c 08    movl   $0x80cf872,(%esp)
234  8049197:   e8 e4 ea 00 00          call   8057c80 <_IO_printf>
235  804919c:   c9                      leave  
236  804919d:   c3                      ret    
237 
238 0804919e <_ZThn8_N8CSofaBed5SleepEv>: non-virtual thunk to CSofaBed::Sleep()
239  804919e:   83 6c 24 04 08          subl   $0x8,0x4(%esp)
240  80491a3:   eb 01                   jmp    80491a6 <_ZN8CSofaBed5SleepEv>
241  80491a5:   90                      nop       
242 
243 080491a6 <_ZN8CSofaBed5SleepEv>: CSofaBed::Sleep()
244  80491a6:   55                      push   %ebp
245  80491a7:   89 e5                   mov    %esp,%ebp
246  80491a9:   83 ec 18                sub    $0x18,%esp
247  80491ac:   c7 04 24 90 f8 0c 08    movl   $0x80cf890,(%esp)
248  80491b3:   e8 c8 ea 00 00          call   8057c80 <_IO_printf>
249  80491b8:   c9                      leave  
250  80491b9:   c3                      ret    
251 
252 080491ba <_ZN8CSofaBed9GetHeightEv>: CSofaBed::GetHeight()
253  80491ba:   55                      push   %ebp
254  80491bb:   89 e5                   mov    %esp,%ebp
255  80491bd:   8b 45 08                mov    0x8(%ebp),%eax
256  80491c0:   8b 40 14                mov    0x14(%eax),%eax
257  80491c3:   5d                      pop    %ebp
258  80491c4:   c3                      ret    
259  80491c5:   90                      nop
260  80491c6:   90                      nop
261  80491c7:   90                      nop
262  80491c8:   90                      nop
263  80491c9:   90                      nop
264  80491ca:   90                      nop
265  80491cb:   90                      nop
266  80491cc:   90                      nop
267  80491cd:   90                      nop
268  80491ce:   90                      nop
269  80491cf:   90                      nop
270 
271 
272 080cf8c0 <_ZTV8CSofaBed>: vtable for CSofaBed
273  80cf8c0:   00 00 00 00                 
274  80cf8c4:   40 f9 0c 08  typeinfo for CSofaBed                  
275  80cf8c8:   dc 90 04 08  CSofaBed::~CSofaBed()
276  80cf8cc:   6c 91 04 08  CSofaBed::~CSofaBed()                  
277  80cf8d0:   d8 8f 04 08  CSofa::GetColor()
278  80cf8d4:   8a 91 04 08  CSofaBed::SitDown()                
279  80cf8d8:   a6 91 04 08  CSofaBed::Sleep()                                  
280  80cf8dc:   ba 91 04 08  CSofaBed::GetHeight()  
281  80cf8e0:   f8 ff ff ff                                         
282  80cf8e4:   40 f9 0c 08  typeinfo for CSofaBed 
283  80cf8e8:   d2 90 04 08  non-virtual thunk to CSofaBed::~CSofaBed()
284  80cf8ec:   64 91 04 08  non-virtual thunk to CSofaBed::~CSofaBed()               
285  80cf8f0:   6c 90 04 08  CBed::GetArea()                
286  80cf8f6:   9e 91 04 08  non-virtual thunk to CSofaBed::Sleep()                 
287 
288 080cf8f8 <_ZTV4CBed>: vtable for CBed
289  80cf8f8:   00 00 00 00                 
290  80cf8fc:   68 f9 0c 08 typeinfo for CBed
291  80cf900:   1a 90 04 08 CBed::~CBed()                   
292  80cf904:   4e 90 04 08 CBed::~CBed()                   
293  80cf908:   6c 90 04 08 CBed::GetArea()                 
294  80cf90c:   80 90 04 08 CBed::Sleep()
295 
296 080cf910 <_ZTV5CSofa>: vtable for CSofa
297  80cf910:   00 00 00 00                
298  80cf916:   78 f9 0c 08  typeinfo for CSofa                 
299  80cf918:   86 8f 04 08  CSofa::~CSofa() 
300  80cf91c:   ba 8f 04 08  CSofa::~CSofa()                
301  80cf920:   d8 8f 04 08  CSofa::GetColor()  
302  80cf926:   e4 8f 04 08  CSofa::SitDown()                   
303 
304 080cf928 <_ZTS8CSofaBed>: typeinfo name for CSofaBed
305  80cf928:   38 43 53                cmp    %al,0x53(%ebx)
306  80cf92b:   6f                      outsl  %ds:(%esi),(%dx)
307  80cf92c:   66 61                   popaw  
308  80cf92e:   42                      inc    %edx
309  80cf92f:   65 64 00 00             gs add %al,%fs:%gs:(%eax)
310     ...
311 
312 080cf940 <_ZTI8CSofaBed>: typeinfo for CSofaBed
313  80cf940:   68 ae 0f 08 28          push   $0x28080fae
314  80cf945:   f9                      stc    
315  80cf946:   0c 08                   or     $0x8,%al
316  80cf948:   00 00                   add    %al,(%eax)
317  80cf94a:   00 00                   add    %al,(%eax)
318  80cf94c:   02 00                   add    (%eax),%al
319  80cf94e:   00 00                   add    %al,(%eax)
320  80cf950:   78 f9                   js     80cf94b <_ZTI8CSofaBed+0xb>
321  80cf952:   0c 08                   or     $0x8,%al
322  80cf954:   02 00                   add    (%eax),%al   
323  80cf956:   00 00                   add    %al,(%eax)
324  80cf958:   68 f9 0c 08 02          push   $0x2080cf9
325  80cf95d:   08 00                   or     %al,(%eax)
326     ...
327 
328 080cf960 <_ZTS4CBed>: typeinfo name for CBed
329  80cf960:   34 43                   xor    $0x43,%al
330  80cf962:   42                      inc    %edx
331  80cf963:   65 64 00 00             gs add %al,%fs:%gs:(%eax)
332     ...
333 
334 080cf968 <_ZTI4CBed>: typeinfo for CBed
335  80cf968:   a8 ad                   test   $0xad,%al
336  80cf96a:   0f 08                   invd   
337  80cf96c:   60                      pusha  
338  80cf96d:   f9                      stc    
339  80cf96e:   0c 08                   or     $0x8,%al
340 
341 080cf970 <_ZTS5CSofa>: typeinfo name for CSofa
342  80cf970:   35 43 53 6f 66          xor    $0x666f5343,%eax
343  80cf975:   61                      popa   
344     ...
345 
346 080cf978 <_ZTI5CSofa>: typeinfo for CSofa
347  80cf978:   a8 ad                   test   $0xad,%al
348  80cf97a:   0f 08                   invd   
349  80cf97c:   70 f9                   jo     80cf977 <_ZTS5CSofa+0x7>
350  80cf97e:   0c 08                   or     $0x8,%al 

 

posted on 2013-03-03 18:13  阿加  阅读(390)  评论(0编辑  收藏  举报

导航