2 Replies - 665 Views - Last Post: 14 March 2011 - 02:57 PM

#1 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Function Pointer Example

Posted 19 December 2008 - 12:26 PM

Description: Interesting feature of the C++ language. Implement when necessary. Illustrates how one would assign a function to a pointer and subsequently call it.
#include 
#include 
using namespace std;

void printFunction()				{ cout << "I got called using a pointer!";};
void helloMessage(string name)			{ cout << "Hi " << name << ". I'm a function called by a pointer!";};

int main()
{
	//Pointer to the first function
	void (*ptrOne)() = &printFunction; //syntax: void (*ptrName) (param type) = &functionName
	(*ptrOne)(); //calls printFunction
	cout << endl;

	//Another example
	void (*ptrTwo)(string) = &helloMessage;
	string name;
	cout << "nEnter your first name: ";
	cin >> name;
	(*ptrTwo)(name);
	cout << endl;

	//Alternately, you can assign a function without the use of the address operator
	//and derefencing the pointer is optional, see below

	cout << endl;

	void (*ptrThree)() = printFunction; //syntax: void (*ptrName) (param type) = functionName
	ptrThree(); //calls printFunction
	cout << endl;

	//Another example
	void (*ptrFour)(string) = helloMessage;
	cout << "nEnter your first name: ";
	cin >> name;
	ptrFour(name);
	cout << endl;

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Function Pointer Example

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Function Pointer Example

Posted 19 December 2008 - 12:26 PM

Description: Interesting feature of the C++ language. Implement when necessary. Illustrates how one would assign a function to a pointer and subsequently call it.
#include 
#include 
using namespace std;

void printFunction()				{ cout << "I got called using a pointer!";}
void helloMessage(string name)			{ cout << "Hi " << name << ". I'm a function called by a pointer!";}

int main()
{
	//Pointer to the first function
	void (*ptrOne)() = &printFunction; //syntax: void (*ptrName) (param type) = &functionName
	(*ptrOne)(); //calls printFunction
	cout << endl;

	//Another example
	void (*ptrTwo)(string) = &helloMessage;
	string name;
	cout << "nEnter your first name: ";
	cin >> name;
	(*ptrTwo)(name);
	cout << endl;

	//Alternately, you can assign a function without the use of the address operator
	//and derefencing the pointer is optional, see below

	cout << endl;

	void (*ptrThree)() = printFunction; //syntax: void (*ptrName) (param type) = functionName
	ptrThree(); //calls printFunction
	cout << endl;

	//Another example
	void (*ptrFour)(string) = helloMessage;
	cout << "nEnter your first name: ";
	cin >> name;
	ptrFour(name);
	cout << endl;

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Function Pointer Example

Posted 14 March 2011 - 09:42 AM

I found this after hopelessly trying to understand what they are, so its good :) but how come there are semi-colons after your functions?
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Function Pointer Example

Posted 14 March 2011 - 02:57 PM

I have no idea, oversight probably. Glad it was helpful!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1