![]() |
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 |
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 |
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 :
:
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? |
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.
|
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.
|
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. |
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. |
Re: Pointer to member function
OK, problem solved with static member functions. But problem persists with non-static ones :(
Any ideas? |
Re: Pointer to member function
You have already gotten the answer. It is impossible to do what you want to do by using C++.
|
Re: Pointer to member function
Quote:
Remember, this is C++. Nothing is impossible. :) |
| All times are GMT -5. The time now is 12:48 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC