Originariamente inviato da giuseppe500
1)void h(void (*pf)(int*)) {} dichiara un puntatore alla funzione f ?ma perchè questa sintassi , non riesco bene a comprendere.
scusa ma a me sembra che con:
codice:
void h(void (*pf)(int*)) {}
non venga dichiarato alcun puntatore alla funzione f, ma bensì venga definita la funzione h di tipo void che ha per argomento un puntatore a funzione (di tipo void che ha per argomento un puntatore a int).
Facciamo un prova giusto per conferma:
codice:
/*
Isidoro Ghezzi
$ g++ --version; g++ -ansi -pedantic -Wextra -Wconversion main.cpp; ./a.out
i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The value is: 2010;
*/
#include <iostream>
void test (int * thePtr){
std::cout << "The value is: " << (*thePtr) << ";" << std::endl;
}
void h(void (*pf)(int*)){
int a = 2010;
pf (&a);
}
int main (void){
h (test);
return 0;
}
confermato ;-)
EDIT:
che è equivalente a (in modo più leggibile ricorrendo al typedef):
codice:
/*
Isidoro Ghezzi
$ g++ --version; g++ -ansi -pedantic -Wextra -Wconversion main.cpp; ./a.out
i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The value is: 2010;
*/
#include <iostream>
void test (int * thePtr){
std::cout << "The value is: " << (*thePtr) << ";" << std::endl;
}
typedef void (*PointerToAFunctionWithAnIntegerPointerAsArg) (int * theIntegerPointer);
void h(PointerToAFunctionWithAnIntegerPointerAsArg pf){
int a = 2010;
pf (&a);
}
int main (void){
h (test);
return 0;
}