Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/Functor

    v1 v2  
    6767
    6868== Types ==
     69There are two types of Functors: Functors for static functions and Functors for member functions. The following sections explain the difference.
     70
    6971=== FunctorStatic ===
     72C++ knows two types of static functions:
     73
     74Static functions in a class:
     75{{{
     76class SomeClass
     77{
     78    static void someFunction();
     79};
     80}}}
     81And C functions outside of a class:
     82{{{
     83void someOtherFunction();
     84}}}
     85
     86Both types are covered by FunctorStatic. FunctorStatic is in fact just a Functor, but you can use it to force static functions:
     87{{{
     88class SomeClass
     89{
     90    static void staticFunction();
     91    void nonstaticFunction();
     92};
     93
     94FunctorStatic* functor1 = createFunctor(&SomeClass::staticFunction);    // this works
     95FunctorStatic* functor2 = createFunctor(&SomeClass::nonstaticFunction); // this fails
     96}}}
     97However, this would work:
     98{{{
     99Functor* functor2 = createFunctor(&SomeClass::nonstaticFunction);
     100}}}
    70101
    71102=== FunctorMember ===
     103There are two types of member functions: constant and non-constant.
     104{{{
     105class SomeClass
     106{
     107    void constFunction() const;
     108    void nonconstFunction();
     109};
     110}}}
    72111
    73 == Template ==
     112Both types are covered by FunctorMember, but in some cases you have to be aware of the intern difference.
     113
     114FunctorMember is in fact just a Functor, but it needs an object to call the function. Just pass the object in front of the arguments when calling the function:
     115{{{
     116class SomeClass
     117{
     118    void someFunction(int value);
     119};
     120
     121SomeClass* object = new SomeClass();
     122FunctorMember* functor = createFunctor(&SomeClass::someFunction);
     123
     124(*functor)(object, 10); // this is equivalent to object->someFunction(10);
     125}}}
     126
     127You can bind an object to a Functor by adding the object with '''setObject('''''object''''')'''. If an object is bound to a Functor, you can call the functor without passing the object again:
     128{{{
     129class SomeClass
     130{
     131    void someFunction(int value);
     132};
     133
     134SomeClass* object = new SomeClass();
     135FunctorMember* functor = createFunctor(&SomeClass::someFunction);
     136
     137function->setObject(object); // binds object to the Functor
     138
     139(*functor)(10); // this is equivalent to object->someFunction(10);
     140}}}
     141
     142Note: If you add a constant object, you can only call the Functor if the assigned function-pointer leads to a constant function too.
     143
     144== Template and ambiguity ==
    74145
    75146== Examples ==