| | 72 | C++ knows two types of static functions: |
| | 73 | |
| | 74 | Static functions in a class: |
| | 75 | {{{ |
| | 76 | class SomeClass |
| | 77 | { |
| | 78 | static void someFunction(); |
| | 79 | }; |
| | 80 | }}} |
| | 81 | And C functions outside of a class: |
| | 82 | {{{ |
| | 83 | void someOtherFunction(); |
| | 84 | }}} |
| | 85 | |
| | 86 | Both types are covered by FunctorStatic. FunctorStatic is in fact just a Functor, but you can use it to force static functions: |
| | 87 | {{{ |
| | 88 | class SomeClass |
| | 89 | { |
| | 90 | static void staticFunction(); |
| | 91 | void nonstaticFunction(); |
| | 92 | }; |
| | 93 | |
| | 94 | FunctorStatic* functor1 = createFunctor(&SomeClass::staticFunction); // this works |
| | 95 | FunctorStatic* functor2 = createFunctor(&SomeClass::nonstaticFunction); // this fails |
| | 96 | }}} |
| | 97 | However, this would work: |
| | 98 | {{{ |
| | 99 | Functor* functor2 = createFunctor(&SomeClass::nonstaticFunction); |
| | 100 | }}} |
| 73 | | == Template == |
| | 112 | Both types are covered by FunctorMember, but in some cases you have to be aware of the intern difference. |
| | 113 | |
| | 114 | FunctorMember 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 | {{{ |
| | 116 | class SomeClass |
| | 117 | { |
| | 118 | void someFunction(int value); |
| | 119 | }; |
| | 120 | |
| | 121 | SomeClass* object = new SomeClass(); |
| | 122 | FunctorMember* functor = createFunctor(&SomeClass::someFunction); |
| | 123 | |
| | 124 | (*functor)(object, 10); // this is equivalent to object->someFunction(10); |
| | 125 | }}} |
| | 126 | |
| | 127 | You 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 | {{{ |
| | 129 | class SomeClass |
| | 130 | { |
| | 131 | void someFunction(int value); |
| | 132 | }; |
| | 133 | |
| | 134 | SomeClass* object = new SomeClass(); |
| | 135 | FunctorMember* functor = createFunctor(&SomeClass::someFunction); |
| | 136 | |
| | 137 | function->setObject(object); // binds object to the Functor |
| | 138 | |
| | 139 | (*functor)(10); // this is equivalent to object->someFunction(10); |
| | 140 | }}} |
| | 141 | |
| | 142 | Note: 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 == |