RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 8794 | Replies: 3
Reply
Join Date: Jan 2006
Posts: 2
Reputation: MaaleaRogers is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MaaleaRogers MaaleaRogers is offline Offline
Newbie Poster

C++ Passing Function to Function

  #1  
Jan 10th, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: C++ Passing Function to Function

  #2  
Jan 10th, 2006
#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 );
}
Reply With Quote  
Join Date: Jan 2006
Posts: 7
Reputation: NosKills is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
NosKills NosKills is offline Offline
Newbie Poster

Re: C++ Passing Function to Function

  #3  
Jan 10th, 2006
#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;
}
Reply With Quote  
Join Date: Jan 2006
Posts: 2
Reputation: MaaleaRogers is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MaaleaRogers MaaleaRogers is offline Offline
Newbie Poster

Re: C++ Passing Function to Function

  #4  
Jan 10th, 2006
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:15 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC