apuntadores a funciones
Hola. Como creo un apuntador a una función?. Lo necesito para que sea un parámetro de otra función el cual indique que función usar dentro de esta nueva funcion.
Gracias.
Gracias.
Te mando como ejemplo una programa con una funcion Swap() que intercambia entre si dos pointers a funciones. Espero que te sea util.
// program k9d1.CPP - page 205
// pointers to functions swapping.
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
int F1(int a,int b)
{
return a + b;
} // F1
int F2(int a,int b)
{
return a - b;
} // F2
void Swap(int(**pa)(int,int),int(**pb)(int,int))
{
int (*pc)(int,int) = *pa;
*pa = *pb;
*pb = pc;
} // SWAP
void Process()
{
int (*pF1)(int,int) = F1,
(*pF2)(int,int) = F2;
cout << "F1 = " << pF1(5,3) << endl;
cout << "F2 = " << pF2(5,3) << endl << endl;
Swap(&pF1,&pF2);
cout << "F1 = " << pF1(5,3) << endl;
cout << "F2 = " << pF2(5,3) << endl;
} // PROCESS
void main()
{
clrscr();
cout << "pointers to functions swapping.n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";;
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
end of program - good bye ! ! !
*/
// program k9d1.CPP - page 205
// pointers to functions swapping.
// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
int F1(int a,int b)
{
return a + b;
} // F1
int F2(int a,int b)
{
return a - b;
} // F2
void Swap(int(**pa)(int,int),int(**pb)(int,int))
{
int (*pc)(int,int) = *pa;
*pa = *pb;
*pb = pc;
} // SWAP
void Process()
{
int (*pF1)(int,int) = F1,
(*pF2)(int,int) = F2;
cout << "F1 = " << pF1(5,3) << endl;
cout << "F2 = " << pF2(5,3) << endl << endl;
Swap(&pF1,&pF2);
cout << "F1 = " << pF1(5,3) << endl;
cout << "F2 = " << pF2(5,3) << endl;
} // PROCESS
void main()
{
clrscr();
cout << "pointers to functions swapping.n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";;
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
end of program - good bye ! ! !
*/
