![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Pointer to member function
Hello,
There is a quite problematic issue I would like to have solved. Consider an event handling function for a button. This function takes, among it's arguments, a pointer to a function, which it will execute if the button is pressed. Let's say I have a number of objects of class C, and I would like to do an operation on one of them if the button is pressed. I cannot pass "&object.operation" as a parameter to the button handling function, because the c++ standard doesn't allow that. Instead, I have to pass "&C::action". But passing this will not modify the one object I want to have modified when the user clicks on the button. Is there a way to pass a function pointer towards the method of THE object I want to modify? Thank you |
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 296
Rep Power: 4
![]() |
Re: Pointer to member function
Unless the function you will call isn't static you have to pass a pointer to the object too. Something like this maybe. This code may not compile but you probably get the idea. Not sure about the member function syntax.
class ButtonCallbackListener
{
public:
};
class Button
{
public:
Button(ButtonCallbackListener* object, void (ButtonCallbackListener::*functionPtr)());
void onPressed() { mObject->(*mFunctionPtr)(); }
private:
ButtonCallbackListener* mObject;
void (ButtonCallbackListener::*mFunctionPtr)();
};
class MyClass : public ButtonCallbackListener
{
public:
MyClass() { mButton = new Button(this, &MyClass::callback); }
void callback() {}
private:
Button* mButton;
};
__________________
http://www.klarre.se |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Pointer to member function
Thank you for your reply. However, it's not exactly what I was looking for. I don't think I was clear enough in my question, so here's a simple example :
cplusplus Syntax (Toggle Plain Text)
So what I want to do, is execute an action ON ANY KIND OF SPECIFIC OBJECT i want, when the button is pressed. Which is not allowed by the C++ ISO, which says : Quote:
Hope you guys understand better now ![]() Any suggestions? |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
Re: Pointer to member function
Klarre understood you correctly; you're trying to do something invalid. A pointer to member function does not carry any information about what instance of the class it will act on (i.e. the object), so you need to pass that information separately.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Pointer to member function
Yes, but the thing is, I don't want to have an ObjectA or an ObjectB as private members of the Button class. The code must work for any kind of object there is. In other terms, the button handling code must not know which object it will be performing an operation on. It could be an int, a Dog, a Cat, or another Button.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
Re: Pointer to member function
Who says you have to? Either pass two arguments, or pass a structure that contains both pieces of information (the address of whatever you need to act on, and the function that acts on it). You're already passing one of those bits of information to your Button class (and presumably storing it somewhere, such as with a member of the Button) so why not pass the complete set of information needed?
If I asked you to put fruit into a jar, you'd expect me to tell you where the fruit is and where the jar is. If I only tell you where the fruit is, you probably won't succeed in putting it into the jar. You're trying to give your software (your Button class) the fruit, while throwing the jar away. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Pointer to member function
You're right! Thanks! So what you suggest, is to have some kind of CallbackListener class from which the objects I want to manipulate inside the button handling code inherit? That would solve my problem. But, in a more general way, how would you do it? I mean to pass the address of an int for example, which is does not inherit from a CallbackListener class...the Button class does not know what object it will receive as an argument...unless the Butoon class is a template class. But let's not go that far
![]() EDIT : removed unnecesary content. Last edited by Lakrids; Feb 10th, 2008 at 6:15 AM. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Pointer to member function
OK, problem solved with static member functions. But problem persists with non-static ones
![]() Any ideas? |
|
|
|
|
|
#9 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 296
Rep Power: 4
![]() |
Re: Pointer to member function
You have already gotten the answer. It is impossible to do what you want to do by using C++.
__________________
http://www.klarre.se |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Dec 2007
Posts: 28
Rep Power: 0
![]() |
Re: Pointer to member function
Quote:
But a fellow programmer just pointed me out that in fact, it is possible. And he wrote me code which works. The idea is to make a static wrapper member function which will call the non-static one. It also involves passing a pointer to the object, which is then converted into a void*, and in the member function itself, reconverted into the class's type, which will then be passed to the function as the first argument, the implicit "this". This way the wanted action is performed on the object in question, and always gives accurate results.Remember, this is C++. Nothing is impossible. ![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Incompatible pointer type (double pointer in a function) | Gabriel Margarido | C | 16 | Nov 23rd, 2007 3:04 AM |
| Get member function address | Klarre | C++ | 3 | Mar 28th, 2007 6:34 AM |
| Function pointer to a member function | Jimbo | C++ | 3 | May 12th, 2006 4:53 PM |
| Need help writing a member function | Rawr101 | C++ | 3 | Apr 11th, 2006 3:18 AM |
| function pointer | raom | C | 9 | Nov 18th, 2005 2:42 PM |