国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++實現(xiàn)動態(tài)綁定代碼分享

2020-05-23 14:13:05
字體:
供稿:網(wǎng)友

對于C++動態(tài)綁定的理解,就是編譯器用靜態(tài)分析的方法加上虛擬函數(shù)的設計實現(xiàn)在程序運行時動態(tài)智能執(zhí)行正確虛擬函數(shù)的技術。要徹底理解動態(tài)綁定,只需要掌握兩點,一是編譯器的靜態(tài)編譯過程,二是虛擬函數(shù)的基本知識。只要有了這兩點理解,任何動態(tài)綁定的分析都是很容易的

C++實現(xiàn)動態(tài)綁定代碼分享

 

 
  1. #include <iostream> 
  2. #include<string> 
  3. using namespace std; 
  4. class BookItem 
  5. private
  6. string bookName; 
  7. size_t cnt; 
  8. public
  9. BookItem(const string&s,size_t c,double p): 
  10. bookName(s),cnt(c),price(p) 
  11. {} 
  12. ~BookItem(){} 
  13. protected
  14. double price; 
  15. public
  16. double bookPrice() 
  17. return this->price; 
  18. string getBookName() 
  19. return this->bookName; 
  20. size_t getBookCount() 
  21. return this->cnt; 
  22. virtual double money() 
  23. return cnt*price; 
  24. virtual void costMoney() 
  25. cout<<money()<<endl; 
  26. }; 
  27. class BookBatchItem:public BookItem 
  28. private
  29. string bookName; 
  30. size_t cnt; 
  31. public
  32. BookBatchItem(const string&s,size_t c,double p,double discountRate): 
  33. BookItem(s,c,p),cnt(c),discount(discountRate) 
  34. {} 
  35. ~BookBatchItem(){} 
  36. private
  37. double discount; 
  38. public
  39. double money() 
  40. if(cnt>=10) 
  41. return cnt*price*(1.0-discount); 
  42. else 
  43. return cnt*price; 
  44. void costMoney() 
  45. cout<<money()<<endl; 
  46. // cout<<cnt<<endl; 
  47. // cout<<price<<endl; 
  48. // cout<<discount<<endl; 
  49. // cout<<"..."<<endl; 
  50. }; 
  51. int main() 
  52. BookItem b1("Uncle Tom's house",11,12.5); 
  53. b1.costMoney(); 
  54. BookBatchItem b2("Gone with wind",11,12.5,0.12); 
  55. b2.costMoney(); 
  56. BookItem* pb=&b1; 
  57. pb->costMoney(); 
  58. pb=&b2; 
  59. pb->costMoney(); 
  60. return 0; 

只有采用“指針->函數(shù)()”或“引用.函數(shù)()”的方式調(diào)用C++類中的虛函數(shù)才會執(zhí)行動態(tài)綁定,非虛函數(shù)并不具備動態(tài)綁定的特征,不管采用任何方式調(diào)用都不行。

下面代碼中,一個java或者C#的程序員容易犯的一個錯誤。

 

 
  1. class Base 
  2. public
  3. Base() { p = new char ; } 
  4. ~Base() { delete p; } 
  5. private
  6. char * p ; 
  7. }; 
  8.  
  9. class Derived:public Base 
  10. public
  11. Derived() { d = new char[10] ; } 
  12. ~Derived() { delete[] d; } 
  13. private
  14. char * d ; 
  15. }; 
  16.  
  17. int main() 
  18. Base *pA = new Derived(); 
  19. delete pA ; 
  20.  
  21. Derived *pA = new Derived(); 
  22. delete pA ; 

代碼中:

執(zhí)行delete pA時,直接執(zhí)行~Base析構(gòu)函數(shù),不會執(zhí)行~Derived析構(gòu)函數(shù)的,原因在于析構(gòu)函數(shù)不是虛函數(shù)。

執(zhí)行delete pB時,先執(zhí)行~Derived()然后再執(zhí)行~Base()。

相比之下,java和C#中,所有的函數(shù)調(diào)用都是動態(tài)綁定的。

關于C++的成員函數(shù)調(diào)用與綁定方式,可以通過下面的代碼測試:

 

 
  1. class Base 
  2. public
  3. virtual void Func() { cout<<"Base"<<endl; } 
  4. }; 
  5.  
  6. class Derived:public Base 
  7. public
  8. virtual void Func() { cout<<"Derived"<<endl; } 
  9. }; 
  10.  
  11. int main() 
  12. Derived obj; 
  13. Base * p1 = &obj; 
  14. Base & p2 = obj; 
  15. Base obj2 ; 
  16.  
  17. obj.Func() ; //靜態(tài)綁定,Derived的func 
  18. p1->Func(); //動態(tài)綁定,Derived的func 
  19. (*p1).Func(); //動態(tài)綁定,Derived的func 
  20. p2.Func(); //動態(tài)綁定,Derived的func 
  21. obj2.Func(); //靜態(tài)綁定,Base的func 
  22.  
  23. return 0 ; 

可以看出“對象名.函數(shù)()”屬于靜態(tài)綁定,當然,使用指針轉(zhuǎn)換為對象的方式應該屬于指針調(diào)用那一類了,至于“類名::函數(shù)()”毫無疑問屬于靜態(tài)綁定。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 寻乌县| 武强县| 德清县| 鄂尔多斯市| 宁阳县| 根河市| 兰西县| 西乌珠穆沁旗| 翁牛特旗| 临泉县| 武乡县| 昭通市| 黔江区| 逊克县| 石首市| 曲松县| 长春市| 无棣县| 广宁县| 赤水市| 墨玉县| 隆尧县| 清徐县| 眉山市| 琼海市| 宁乡县| 滕州市| 隆化县| 博客| 富顺县| 东宁县| 集贤县| 西吉县| 南丰县| 汉阴县| 横峰县| 朝阳县| 扎兰屯市| 元江| 苏尼特左旗| 阿拉善盟|