Advertisement

Function Pointer+array+member function=mess

Started by May 26, 2001 05:01 AM
1 comment, last by DrjonesDW3d 23 years, 8 months ago
Hi, I am tring to write some code in visual C++ 6 that will create an array of function pointers, where the functions pointed to are member functions of a class. I know i can declare an array of function pointesr as follows (this worked for me for just regular global functions in a small console app)
    	
	void (*FuncPointer[2])(int);

	FuncPointer[0]=myFunction1;
	FuncPointer[1]=myFunction2;

	FuncPointer[0](1);
	FuncPointer[1](1);
    
But when i tried to use member functions (by placing the array as a public member varriable in the class) It caused errors. I read some info on other ways to do it but none seemed to fix the problem without causing more. It doesn't matter if the array is global or a member function... just as long as the function pointers can point to member functions (the need to access member varriables) I have tried all sorts of things with ::* and &clsengine::function, but can't get anything to work. I just started using function pointers so i know the basics, but does anyone know how to create an array of function pointers that point to functions that are member functions of a class? Or a site that has a good tutorial on function pointers so i might be able to figure it out? Thanks for the help. Dr. Jones Edited by - DrjonesDW3d on May 26, 2001 6:02:17 AM
i dont think you can .. i tried before, but methods in a class have a different name declarative compared to just normal functions ..

try to make friennd functions and point to them..

     void abc(LPVOID lp){  ((myclass)lp)->bcd();} class myclass {  friend void abc();  void bcd();}    


just a suggestion to put the function pointer to bcd..
unless somebody else can find a way to point to a method, i certainly cant..

function pointer

{ Stating the obvious never helped any situation !! }

Edited by - jwalker on May 26, 2001 9:01:59 AM
Advertisement
There''s no need to make friend functions if you want pointers to a class member function. What you need to do is to get the type of a pointer-to-member-function right.

Example:

class a
{
public:
void foo(int);
void bar(int);
};

If you want a pointer to a member function expecting an int, this is the correct type:

void (a::*)(int)

To make an easy typedef:

typedef void (a::*FuncPointer)(int);

FuncPointer now points to a member function of a that expects an int.

To call a member function using a function pointer:

FuncPointer fptr = &a::foo;
a aobj;
(aobj.*fptr)(1);

You can make an array like this:

FuncPointer array[] = { &a::foo, &a::bar };

or

void (a::*array[])(int) = { &a::foo, &a::bar };

Calling works as follows:

(aobj.*(array[0]))(1);

For info, see http://www.parashift.com/cpp-faq-lite/pointers-to-members.html, which goes a bit more into detail about pointers to member functions.

The technique jwalker points out can be used to use a class member function in code that expects a pointer to a global function.

HTH

This topic is closed to new replies.

Advertisement