正在加载图片...
Implement a inheritance hierarchy(cont.) a Virtual function If certain function has different implementations in the inheritance hierarchy declare it as virtual Dynamic binding makes the corresponding version of virtual function called according to the real object type If there's virtual member function in the class the destructor should also be declared as virtual class Base void fo Base* p= new Derived virtual -Base( delete p; ; I/virtual destructor ensures resources class Derived public Base llin derived classes could be released correctly in polymorphism Derived(0;Implement a inheritance hierarchy (cont.) ◼ Virtual function ◼ If certain function has different implementations in the inheritance hierarchy, declare it as virtual ◼ Dynamic binding makes the corresponding version of virtual function called according to the real object type ◼ If there’s virtual member function in the class, the destructor should also be declared as virtual class Base { // ... virtual ~Base(); }; class Derived : public Base { // ... ~Derived(); }; void f() { Base* p = new Derived; delete p; //virtual destructor ensures resources //in derived classes could be released //correctly in polymorphism }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有