overloaded operator() in c++ structure -


in effective c++, 3rd edition, page 173~175, scott meyers talked of alternatives virtual functions using strategy pattern:

class gamecharacter; int defaulthealthcalc(const gamecharacter& gc);  class gamecharacter { public:     typedef std::tr1::function<int (const gamecharacter&)> healthcalcfunc;     explicit gamecharacter(healthcalcfunc hcf = defaulthealthcalc)     : healthfunc(hcf)     {}      int healthvalue const     { return healthfunc(*this) }     ... private:     healthcalcfunc healthfunc; }; ... struct healthcalculator {     int operator()(const gamecharacter&) const     { ... } }; ... class eyecandycharacter: public gamecharacter {     explicit eyecandycharacter(healthcalcfunc hcf=defaulthealthcalc)     :gamecharacter(hcf)     { ... }     ... } ... eyeccandycharacter ecc(healthcalculator()); 

the last statement illustrate how use health calculation function object in constructor of eyecandycharacter class.

my question is, constructor of eyecandycharacter class requires function takes parameter compatible const gamecharacter& , returns convertible int.

is supported/implemented operator() defined in struct healthcalculator? don't quite understand meaning of overloaded operator here.

another question of mine here initializer list in constructor of derived class initializes data members of (though know base part of derived class intialized implicitly). how come base class gamecharacter appears in initializer of derived class eyecandycharacter?

in first question:

my question is, constructor of eyecandycharacter class requires function takes parameter compatible const gamecharacter& , returns convertible int. ... supported/implemented operator() defined in struct healthcalculator?

yes, supported. must know/remember healthcalculator functor. implements operator () "simulate" syntax of calling traditional functions. operator () takes const gamecharacter& , returns int, compatible eyecandycharacter (and subsequently gamecharacter) wants.

in second question:

how come base class gamecharacter appears in initializer of derived class eyecandycharacter?

that initializes base class of eyecandycharacter gamecharacter calling gamecharacter's constructor. not doing makes eyecandycharacter's constructor call gamecharacter's default constructor, isn't defined, , therefore result in error.


as sidenote, can now, in c++11, directly use std::function have same functionality std::tr1::function.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -