pointer to diferent member functions

Started by
2 comments, last by DMINATOR 18 years, 9 months ago
Is it possible , to make a pointer that can point to different member functions ? I know that I can create a pointer to a member function like that :

#include <stdio.h>

int temp = 0;

class Test
{
public:

	void Start()
	{
		temp = 20;
	};
};

//to class
void (Test::*pt2Func3)(void);


int main()
{

	 pt2Func3 = &Test::Start;


	 printf("temp = %d\n",temp);


	 //create the class we wan't to use with
	 Test test;

	 //let's call the function then
	 (test.*pt2Func3)();

	 printf("temp = %d\n",temp);

	return 0;
}

But if for example I have 2 different classes and I wan't to make a pointer that can point to any member function in these classes. Can it be done ? Thanks for any input.
Advertisement
C++ doesn't natively support that kind of pointer. You may want to consider a functor implementation instead such as boost::function.
Loki::Functor is good, too.

my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Ok thanks, for the quick answer :).

This topic is closed to new replies.

Advertisement