Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2008, 12:11 PM   #1
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
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
Lakrids is offline   Reply With Quote
Old Feb 9th, 2008, 1:04 PM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Old Feb 9th, 2008, 4:51 PM   #3
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
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)
  1. class Button {
  2. public :
  3. Button(etc, etc, etc, void (*action)());
  4. void onPressed() { *action(); }
  5. };
  6.  
  7. void randomAction() {
  8. // Some code
  9. }
  10.  
  11. int main() {
  12.  
  13. ObjectA A;
  14. ObjectA AA;
  15. ObjectB B;
  16. ObjectC C;
  17.  
  18. Button b(etc, etc, etc, &A.action); // isn't allowed by C++ ISO
  19. Button b(etc, etc, etc, &B.action); // isn't allowed by C++ ISO
  20. Button b(etc, etc, etc, &randomAction); // is allowed by C++ ISO
  21.  
  22. // Other code
  23.  
  24. return 0;
  25. }

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:
error : ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&ObjectA::action’
But saying "&ObjectA::action" WILL NOT alter the specific object ("A" in this case), it will only call ObjectA::action(), not A.action().

Hope you guys understand better now

Any suggestions?
Lakrids is offline   Reply With Quote
Old Feb 9th, 2008, 7:41 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Feb 10th, 2008, 5:14 AM   #5
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
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.
Lakrids is offline   Reply With Quote
Old Feb 10th, 2008, 5:43 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Feb 10th, 2008, 6:03 AM   #7
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
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.
Lakrids is offline   Reply With Quote
Old Feb 10th, 2008, 11:16 AM   #8
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Re: Pointer to member function

OK, problem solved with static member functions. But problem persists with non-static ones

Any ideas?
Lakrids is offline   Reply With Quote
Old Feb 10th, 2008, 12:32 PM   #9
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Old Feb 10th, 2008, 4:55 PM   #10
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Re: Pointer to member function

Quote:
It is impossible to do what you want to do by using C++.
Lol thanks! 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.
Lakrids is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC