Almost every more or less serious project uses callbacks. Sometimes programmer wants to pass externally defined arguments to callback function. The way you may chose is to define additional arguments to function that executes callback: c++:
void call(void (*f)(int), int a){f(a);} void f(int){} .... call(f, 1);python:
def call(f, a): f(a) def f(a): pass call(f, 1)The other way that seemed to me more flexible is to use functors: c++:
template<typename T>void call(T f){f();} class A{public: A(int a){} void operator()(){}}; .... call(A(1));python:
def call(f, a): f(a) class A: def __init__(s, a): pass def __call__(s): pass call(A(1))Using functors you can use a benefit of 3 calls: constructor, call operator and destructor. But you lose performance and memory. As usual programmer have to deal with (memory/performance/code support) issues =).
No comments:
Post a Comment