CS246 Lecture Notes - Lecture 21: X Window System, Method Overriding, Function Overloading
Document Summary
6 virtual void enemy::strike(stick&) = 0; virtual void enemy::strike(rock &) = 0; Line ** is an ambiguous call as c++ does not dynamically dispatch based on arguments. Visitor design pattern: double dispatch combination of method overriding and method overloading sending different types of parameters to the same method name. 9 class enemy public: virtual void strike(weapon &) = 0; class turtle: public enemy public: void strike(weapon &w) override. 11 w. useon(*this); // this has to be a turtle. 19 w. useon(*this); // this has to be a bullet. Using this pattern ensures we use the correct type. strike() should not be in. 9 class weapon public: virtual void useon(turtle &) = 0; // known at compile time when called from turtle. strike virtual void useon(bullet &) = 0; class rock : public weapon public: The visitor design pattern can be used to implement virtual functionality without modifying the class hierarchy. But the class hierarchy must already be setup to accept visitors.