c++ - Function variable instead of pointer to function -


there code:

void a() { }  // function parameter first defined function, pointer function void g(void f() = a, void (*d)() = a) { }  int main(){    void (*z)() = a; // works ok    //void z() = a; error: illegal initializer (only variables can initialized)    return 0; } 

in function parameter list can define function parameter function or pointer function (see function g parameter list). in main function pointer function version seems work. there way define function variable (not pointer function) in block scope (like void f() = a variable in g function)?

you cannot have variables of function type in c++. can either have pointers or references functions, though:

typedef void (funtype)();  void call(funtype & f) { f(); } // ok, funtype & == void (&)() void call(funtype * g) { g(); } // ok, funtype * == void (*)() 

in sense, always call function through function pointer: expression value function decays corresponding function pointer. in above examples, f(), (*f)(), (**f)() , forth valid ways of calling function, , likewise g. passing reference has advantage can still function pointer explicitly saying &f.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -