Function pointers pointing to class functions?

Started by
29 comments, last by Roof Top Pew Wee 22 years, 5 months ago
quote:Original post by Roof Top Pew Wee
1: Just because a function is in a class rather than outside of one, does it really chage it?

Yes, functions outside of classes and non-static methods have different calling-conventions
quote:
I mean, ovbiously it does in my case, but isn''t there a way to just have a typedef for both functions in and outside of classes?

Not really (unless the method in the class is static, but that seems to not be such a good option for you).
quote:
And what if I have two classes that have a void function and I want to assign one or the other depending on some condition? Do I then have to have a typedef for the function in both class A and class B?

Yes you''ll probably need two different typedefs, unless both classes inherits some base class and you can typedef a pointer to a method in that class.
quote:
2: Also, this solution seems to point to the general function for the class A. But what the function that you are pointing to does work with members of the specific class. Example:

class A
{public:
int x;
void functionA(){x++;}
};

Now say that you have 3 instances of class A like this:

A classA1;
A classA2;
A classA3;

When you set the function in class B equal to A::functionA, well, how would the compiler know which x to add? classA1.x, classA2.x, or classA3.x?

Basically you need an object to ''apply'' the method to. Look at my previous post for an example.
Advertisement
Looks like everybody is replying at the same time
quote:
2: Also, this solution seems to point to the general function for the class A. But what the function that you are pointing to does work with members of the specific class. Example:

class A
{public:
int x;
void functionA(){x++;}
};

Now say that you have 3 instances of class A like this:

A classA1;
A classA2;
A classA3;

When you set the function in class B equal to A::functionA, well, how would the compiler know which x to add? classA1.x, classA2.x, or classA3.x?



To answer your question, you must realize that in order to call a member function via a member function pointer you *must* have a valid object reference (unless of course that member function is static.)

So once again:

B b;

b.setFunction(&A::functionA);
b.CallFunctionA(&classA1);
b.CallFunctionA(&classA2);
b.CallFunctionA(&classA3);

CallFunctionA would look something like:

void B::CallFunctionA(A* p)
{
(p->*functionPointer)();
}

Therefore there is no ambiguity for the compiler.

Just remember, for non-static member functions, you are invoking the member function using a member function pointer and the call through pointer to member operator.



Dire Wolf
www.digitalfiends.com

Edited by - Dire.Wolf on November 9, 2001 6:55:03 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
All I can say is WOW. I had no clue that my problem would be so complex, or at least I had no clue this was the solution. I wanna thank all of you guys for helpin me out. I''m going to have to stare at the code for a while, and then apply it to my GUI, which will be quite the challenge.

--Vic--
The question is what are you trying to accomplish with member function pointers that can''t be done with inheritance and overloading?

Give us an example if you can.

Glad to see you have it sorted out somewhat.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
quote:Original post by Roof Top Pew Wee
Dactylos:
I see. Your code seems to work, but I have a question:

Since I am putting the function in the classB, how would I call it? Would I have to go like this:
classA.(classB.functionPointer)(); ?

Basically yes (though your syntax is wrong, you must use the ''.*'' or ''->*'' operators). You should store both a pointer to a method and an object to apply that method to in B.
quote:
That seems kinda pointless. I mean, if I wanted to call the function from class A, I could just do it directly from A, instead of a big circle like that.

Well, why are you doing this. If you don''t see any gain to it perhaps you should try to accomplish whatever you''re doing some other way.
quote:
I could also include a classA inside classB so it''d be like:

classB.classA.(functionPointer)(); but again, that seems like too much to be doing. Are these the only two ways I could do it?

Well, without knowing what you''re trying to accomplish I can''t really answer that, but regarding method-pointers those are basically the only ways to do it (I would say those two ways are one and the same though).
Seems we are repeating each other Dactylos.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Ok, here''s the purpose of the question. I am programming games, obviously, and I didn''t want to learn Windows programming, so I created my own GUI. Well, I am currently working on a File menu. Like the one you click on and get Open, Close, Save, Exit, and so on. I have created a function to this menu that works like this.
void addItem(char* commandToAdd, plainFunction doThisFunction)

So you would call it like this:

fileMenu.addItem("Save", &WHATEVERFUNCTION)

Well, I have already created a save/load dialog window. And it has an activate function. So what I''d wanna do is be able to do something to this effect:

fileMenu.addItem("Save", &(saveLoadDialog.activate) )

I now realize that the correct method is to do:

fileMenu.addItem("Save", &(fileBox::activate), &saveLoadDialog);

So I''m going to have to create a different version for when a member function is passed so that the function also takes the address of the class to use when calling the function.

I''m thinking I have it down now, but if you guys see any problems, don''t hesitate to tell me. Thanks VERY MUCH!
--Vic--
Dire.Wolf,
Yeah, we seem to be posting at the same time
Read about the "Observer" or "Listener" pattern
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement