Sunday 15 February 2015

C++ Overload an overrided method -



C++ Overload an overrided method -

is possible in c++ overload in kid classes overrided method? i'm asking because have many kid classes although same (in case game objects) interact in different ways each others. so, need create function void processcollision(gameobject obj) in superclass. overloaded in kid classes depending on class of gameobject (if it's building, auto ...).

i'm trying run alternative using upcasting , rtti.

what you're trying implement called "multiple dispatch" , unfortunately c++ doesn't back upwards straight (because in c++ view methods bounded classes , there no multimethods).

any c++ solution require coding implementation.

one simple symmetric way implement create map supported cases:

typedef void (*handler)(obj *a, obj *b); typedef std::map<std::pair<otype, otype>, handler> handlermap; handlermap collision_handlers;

then collision handling is:

handlermap::iterator = collision_handlers.find(std::make_pair(a->type, b->type)); if (i != collision_handlers.end()) i->second(a, b);

and code goes in free function.

if speed key factor , object type can coded in little integer (e.g. 0...255) dispatch become example:

collision_handlers[(a->type<<8)+b->type](a, b);

where collision handler array of function pointers, , speed should equivalent single virtual dispatch.

the wikipedia link @ start of reply lists more sophisticated alternative c++ (the visitor pattern).

c++

No comments:

Post a Comment