c++ - Pointer to functions in a STL map -
i facing problem regarding function pointers syntax. have used these before situation have make things totally not obvious anymore.
so, designing factory creates nodes game. factory's create method has 2 overloads shown here:
- abstractnode* create(abstractnode::type) const;
- abstractnode* create(abstractnode::type, vector3) const;
in factory, have private methods take care of creation process (based on type of node) here 2 methods create ball:
- abstractnode* createball() const;
- abstractnode* createball(vector3) const;
and here both maps in store function pointers. these maps redirect call correct create method (if possible of course)
typedef abstractnode* (*functor)(void); typedef abstractnode* (*functorposition)(vector3); map<unsigned int, functor> functors_; <unsigned int, functorposition> functorsposition_; now, question is, syntax add function pointers in both maps.
here last attempt, doesn't compile , throws error below:
functors_.insert(make_pair(abstractnode::name_ball, &createball)); the error shown:
no instance of function template std::make_pair matches argument list argument types (const unsigned int, <unkown-type>) thanks in advance!
createball non-static member function. pointer-to-member-function types different pointer-to-function types.
so, type of &createball isn't abstractnode* (*)(void), it's abstractnode* (factory::*)(void)const, factory name of class appears in. if want pointer-to-member-function change type functor (and make sure when come call it, have instance of factory call on). if want pointer-to-function change createball static member function.
furthermore, compiler can't work out type of pair want create, since &createball ambiguous between 2 overloads named createball. once have type functor correct, can write:
functors_.insert(make_pair(abstractnode::name_ball, (functor)&createball)); functorsposition_.insert(make_pair(abstractnode::name_ball, (functorposition)&createball)); the cast tells compiler function mean -- special exception usual rule in c++ type of subexpression &createball doesn't depend on context appears in.
i think should able avoid cast with:
functors_[abstractnode::name_ball] = &createball; since assignment of pointer overloaded function likewise special case. i'm lazy check it.
Comments
Post a Comment