首页 > 编程 > C++ > 正文

C++实现动态绑定代码分享

2020-05-23 14:13:05
字体:大 中 小
来源:转载
供稿:网友

对于C++动态绑定的理解,就是编译器用静态分析的方法加上虚拟函数的设计实现在程序运行时动态智能执行正确虚拟函数的技术。要彻底理解动态绑定,只需要掌握两点,一是编译器的静态编译过程,二是虚拟函数的基本知识。只要有了这两点理解,任何动态绑定的分析都是很容易的

C++实现动态绑定代码分享

 

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

只有采用“指针->函数()”或“引用.函数()”的方式调用C++类中的虚函数才会执行动态绑定,非虚函数并不具备动态绑定的特征,不管采用任何方式调用都不行。

下面代码中,一个java或者C#的程序员容易犯的一个错误。

 

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

代码中:

执行delete pA时,直接执行~Base析构函数,不会执行~Derived析构函数的,原因在于析构函数不是虚函数。

执行delete pB时,先执行~Derived()然后再执行~Base()。

相比之下,java和C#中,所有的函数调用都是动态绑定的。

关于C++的成员函数调用与绑定方式,可以通过下面的代码测试:

 

 
  1. class Base 
  2. { 
  3. public: 
  4. virtual void Func() { cout<<"Base"<<endl; } 
  5. }; 
  6.  
  7. class Derived:public Base 
  8. { 
  9. public: 
  10. virtual void Func() { cout<<"Derived"<<endl; } 
  11. }; 
  12.  
  13. int main() 
  14. { 
  15. Derived obj; 
  16. Base * p1 = &obj; 
  17. Base & p2 = obj; 
  18. Base obj2 ; 
  19.  
  20. obj.Func() ; //静态绑定,Derived的func 
  21. p1->Func(); //动态绑定,Derived的func 
  22. (*p1).Func(); //动态绑定,Derived的func 
  23. p2.Func(); //动态绑定,Derived的func 
  24. obj2.Func(); //静态绑定,Base的func 
  25.  
  26. return 0 ; 
  27. } 

可以看出“对象名.函数()”属于静态绑定,当然,使用指针转换为对象的方式应该属于指针调用那一类了,至于“类名::函数()”毫无疑问属于静态绑定。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表