Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 2 and Version 3 of code/doc/Functor


Ignore:
Timestamp:
Sep 30, 2008, 12:56:51 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/Functor

    v2 v3  
    142142Note: If you add a constant object, you can only call the Functor if the assigned function-pointer leads to a constant function too.
    143143
    144 == Template and ambiguity ==
    145 
    146 == Examples ==
     144== Template ==
     145Sometimes you might have to create know the template arguments of a Functor. This may be the case if the name of a function is ambiguous and createFunctor can't decide which function to use.
     146
     147This is how the template arguments are arranged:
     148{{{
     149template <classname, returntype, paramtype1, ..., paramtype5>
     150}}}
     151
     152If a function has no returnvalue, the returntype argument is discarded:
     153{{{
     154template <classname, paramtype1, ..., paramtype5>
     155}}}
     156
     157If a function is static, the classname argument is discarded:
     158{{{
     159template <returntype, paramtype1, ..., paramtype5>
     160}}}
     161
     162If a function has less than 5 parameters, the surplus parameters are discarded:
     163{{{
     164example: 3 parameters:
     165template <paramtype1, paramtype2, paramtype3>
     166}}}
     167
     168Examples:
     169{{{
     170int function(float p1, float p2, float p3);
     171=> <int, float, float, float>
     172
     173void function(const std::string& p);
     174=> <const std::string&>
     175
     176void Class::function(int p);
     177=> <Class, int>
     178
     179bool Class::function(int p);
     180=> <Class, bool, int>
     181}}}
     182
     183== Ambiguity ==
     184Sometimes two functions have the same name but different parameters. This is called overloading. Usually the compiler knows what you want because your arguments define the function. But if you just pass the function-pointer to createFunctor, the compiler has no idea which function to chose.
     185
     186Example:
     187{{{
     188class SomeClass
     189{
     190    void someFunction(const Vector3& position);
     191    void someFunction(float x, float y, float z);
     192};
     193
     194Functor* functor = createFunctor(&SomeClass::someFunction); // error!
     195}}}
     196
     197This problem can be solved by specifying the template arguments by yourself. Read the chapter above to learn about how to arrange the template arguments.
     198
     199This is how it works:
     200{{{
     201class SomeClass
     202{
     203    void someFunction(const Vector3& position);
     204    void someFunction(float x, float y, float z);
     205};
     206
     207Functor* functor1 = createFunctor<SomeClass, const Vector3&>(&SomeClass::someFunction);
     208Functor* functor2 = createFunctor<SomeClass, float, float, float>(&SomeClass::someFunction);
     209}}}