Please support our C++ advertiser: Programming Forums
Views: 8794 | Replies: 3
![]() |
•
•
Join Date: Jan 2006
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Perhaps someone can show me how to fix the following example code so either the function outputx() or outputy() is passed to the function print_it().
void do_it(); // prototype
void outputx(); // prototype
void outputy(); // prototype
void print_it( print_character() ); // prototype
void outputx()
{
printf("x\n")
}
void outputy()
{
printf("y\n")
}
void print_it( print_character() )
{
//effectively becomes either outputx(); or outputy();
print_character();
}
void do_it()
{
// pass the function outputx() to print_it()
print_it( outputx() );
// pass the function outputy() to print_it()
print_it( outputy() );
}
Regards, MaaleaRogers
void do_it(); // prototype
void outputx(); // prototype
void outputy(); // prototype
void print_it( print_character() ); // prototype
void outputx()
{
printf("x\n")
}
void outputy()
{
printf("y\n")
}
void print_it( print_character() )
{
//effectively becomes either outputx(); or outputy();
print_character();
}
void do_it()
{
// pass the function outputx() to print_it()
print_it( outputx() );
// pass the function outputy() to print_it()
print_it( outputy() );
}
Regards, MaaleaRogers
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
#include <stdio.h>
void outputx();
void outputy();
void print_it( void(*)() ); // prototype
void outputx()
{
printf("x\n");
}
void outputy()
{
printf("y\n");
}
void print_it( void(*func)() )
{
//effectively becomes either outputx(); or outputy();
func();
}
int main()
{
// pass the function outputx() to print_it()
print_it( outputx );
// pass the function outputy() to print_it()
print_it( outputy );
}
•
•
Join Date: Jan 2006
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
#include <iostream>
void Function1() { std::cout << "Function1\n"; }
void Function2(void aFunction()) { aFunction(); }
// make a param that describes the function the will be given
// as a param (return type, identifier, paramtypes)
int main()
{
Function1();
std::cout << "\n";
Function2(Function1);
// Function1 is a legal param, it has the same returntype and params as the param in
// function declaration
return 0;
}•
•
Join Date: Jan 2006
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by WolfPack
#include <stdio.h> void outputx(); void outputy(); void print_it( void(*)() ); // prototype void outputx() { printf("x\n"); } void outputy() { printf("y\n"); } void print_it( void(*func)() ) { //effectively becomes either outputx(); or outputy(); func(); } int main() { // pass the function outputx() to print_it() print_it( outputx ); // pass the function outputy() to print_it() print_it( outputy ); }
Thank you, seems like I never get the syntax right.
Regards, MaaleaRogers
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode