getting irritated with function pointers

I understand basic c style function pointers like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <limits>

using namespace std;

float multiply(float x, float y) { return x * y; }


int main(int argc, char *argv[])
{
	float(*myfunction)(float,float) = multiply;

	float x = myfunction(8,8);

	cout << x << endl;

	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n' );
}



However I cant get function pointers that are a member of a class working. both const and non const. can anyone show me.
What have you tried? Can you show us the code where you "cant get function pointers that are a member of a class working"?
I think this is correct. I was really confused with the syntax. its going to take me a few times to remember. below is working correctly now I think. I fought with it the whole way.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

using namespace std;

class test
{
public:
	void tryme() { cout <<  "Test::tryme()" << endl; }
};

void(test::*ptmember)() = &test::tryme;



int main(int argc, char *argv[])
{
	test ts;
	(ts.*ptmember)();

	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n' );
}
I just deleted my post, this is not at all what I thought you were talking about.
more then 6 different trys now to remember syntax w/o peeking. lol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <limits>

using namespace std;

class dinosaur
{
public:
	void roar(int seconds) { cout << "you roar for " << seconds << " seconds" << endl; }
};

void(dinosaur::*doit)(int) = &dinosaur::roar;


int main(int argc, char *argv[])
{
	dinosaur dino;
	(dino.*doit)(10);

	cout << endl;
	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n' );
}


Last edited on
Topic archived. No new replies allowed.