c++ - Polymorphism, Downcasting, and Virtual Functions -
i have code changing class more object-oriented c++ class, ie: less calls inside main loop.
class shape abstract { public: virtual bool check_collision(sdl_point); virtual bool check_collision(sdl_rect); virtual bool check_collision(shape&); protected: }; class adv_object : public object { public: adv_object(shape *obj); virtual void set_shape(shape* obj) { prec_area = obj; } virtual bool check_collision(sdl_point); virtual bool check_collision(sdl_rect); virtual bool check_collision(shape&); protected: shape* prec_area; //better area determination }; where shape purely abstract class, , inherited class, ie: circle.
class circle : public shape { public: void check_collision(sdl_point) override; void check_collision(sdl_rect) override; void check_collision(shape&) override; }; when circle downcasted shape, overloads of virtual functions called or abstract functions called instead?
the program use run-time information (so-called "virtual table") determine virtual function call. done total disregard down/up-casting, in other words - correct overloaded function called.
as side note - if want call specific "incarnation" of virtual function you'd have use explicit name resolution, this:
shape * c = new circle; c->square::check_collision(...); // hope see issues maycause
Comments
Post a Comment