9 Replies - 1329 Views - Last Post: 18 December 2009 - 07:58 AM Rate Topic: -----

#1 Pontus   User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Pointer to function

Post icon  Posted 17 December 2009 - 11:58 AM

Is it possible for a class to contain a pointer to a function, so he can call that function later?
If so, how?
Is This A Good Question/Topic? 0
  • +

Replies To: Pointer to function

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1562
  • View blog
  • Posts: 8,352
  • Joined: 07-September 06

Re: Pointer to function

Posted 17 December 2009 - 12:54 PM

Yes, here is an example of it:

class myClass{
private:
  int(*myFunctionPointer)(int a, int b);
public:
  myClass(int(*myFunc)(int a, int b)){
	myFunctionPointer = myFunc;
  }
};


Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Pointer to function

Posted 17 December 2009 - 12:54 PM

pointer to function or pointer to member function?

making a function pointer as a data member is exactly the same as declaring a function pointer anywhere else, e.g.
class foo
{
	void (*bar)();
}; 

http://c-faq.com/ptrs/funccall.html

Member functions are different however
Read here: http://www.parashift...to-members.html
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Pointer to function

Posted 17 December 2009 - 02:40 PM

Another way that is used quite a bit is to have a function pointer template value:
#include <iostream>
#include <cmath>

template <double (*FuncX)(double) >
class ToFunctor {
	double lastValue;
	public:
	
	ToFunctor(double x) { lastValue = FuncX(x); }
	
	double operator()(double x) {
		lastValue = FuncX(x);
		return lastValue;
	}
	
	double getLastValue() {
		return lastValue;
	}
};

int main() {
	ToFunctor<std::sqrt> squareRoot(1);
	for(int i = 1; i < 10; i++) {
		std::cout << i << ": " << squareRoot(i)  << std::endl;
	}
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Pointer to function

Posted 17 December 2009 - 03:42 PM

I agree functors are generally better than function pointers.

The STL has a few functional tools which can be used to convert ordinary functions into functors; pointer_to_unary_function and pointer_to_binary_function
#include <functional>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>

int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
    std::vector<double> vec;
    std::pointer_to_unary_function<double,double> root(std::sqrt);

    std::transform(arr, arr+10,
                   std::back_inserter(vec),
                   root);

    for(size_t i(0); i!=vec.size(); ++i)
        std::cout << vec.at(i) << std::endl;
}
although unlike the example shown by NickDMax, the functor doesn't maintain any 'state', but its handy for mixing up with STL algorithms
Was This Post Helpful? 0
  • +
  • -

#6 harshapinky   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 09

Re: Pointer to function

Posted 17 December 2009 - 08:19 PM

 
#include<stdio.h>
#include<conio.h>
swap(int *x,int *y)
{
  int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\n %d\n %d",*x,*y);
}
main()
{
 int a,b;
clrscr();
printf("\n enter two nos for swapping");
scanf("%d%d",&a,&b);
swap(&a,&b);
getch();
}



this is a simple example for passing pointers to the functions..(SWAPPING PROGRAM.)hope it is useful :)

This post has been edited by harshapinky: 17 December 2009 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Pointer to function

Posted 17 December 2009 - 09:13 PM

The question was about function pointers not passing pointers to functions
Was This Post Helpful? 0
  • +
  • -

#8 Pontus   User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Pointer to function

Posted 18 December 2009 - 01:03 AM

Thanks for the replies, all!
Was This Post Helpful? 0
  • +
  • -

#9 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Pointer to function

Posted 18 December 2009 - 03:44 AM

@Pontus

Sorry but this is one of those 'fingernails on a blackboard' triggers for me.
I see it so often where I work (from people who speak/write only English) and I live in slight dread that it will become acceptable through sheer quantity of usage.

In your signature block
"Reader discretion is adviced"
should be
"Reader discretion is advised"
('s' not 'c')

I am not trying to have a go at your English language skills or anything similar.
I just have a real problem with that particular spelling error

Now I shall go and have a little lie down and get over myself :D
Was This Post Helpful? 0
  • +
  • -

#10 Pontus   User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Pointer to function

Posted 18 December 2009 - 07:58 AM

Thanks for telling me =P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1