Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function pointer, is this possible?

12 views
Skip to first unread message

lil...@dcccd.edu

unread,
Jun 16, 2009, 1:38:14 PM6/16/09
to
Hopefully I can describe this in a clear manner.

I've created a button class with private member

(int) (* func) (void);

This is set using

void BTN::SetFunction(int (*fnc)(void))
{
func = fnc;
}

This compiles fine. The problem comes when I use SetFunction to
assign the function I want to run. Under straight forward
circumstances this should compile fine. However, the function I want
to set it to is a public member of another class object, bg of class
Grid.

btnNew.SetFunction ( bg.SetNewMode );

But I get a repremand to

"use '&Grid::SetNewMode' to create a pointer to member"

Does this mean that I can only specify a static function of a class?
The more I think about this I can see why the compiler chokes on this.
Aside from providing discete functions to wrap the call to
bg.SetNewMode() is there any way to use a function pointer with a
non-static function of a class?

--
Lilith

Alf P. Steinbach

unread,
Jun 16, 2009, 2:02:56 PM6/16/09
to
* lil...@dcccd.edu:

> Hopefully I can describe this in a clear manner.
>
> I've created a button class with private member
>
> (int) (* func) (void);
>
> This is set using
>
> void BTN::SetFunction(int (*fnc)(void))

Style issue: avoid using all uppercase names except for macros.


> {
> func = fnc;
> }
>
> This compiles fine. The problem comes when I use SetFunction to
> assign the function I want to run. Under straight forward
> circumstances this should compile fine. However, the function I want
> to set it to is a public member of another class object, bg of class
> Grid.
>
> btnNew.SetFunction ( bg.SetNewMode );
>
> But I get a repremand to
>
> "use '&Grid::SetNewMode' to create a pointer to member"
>
> Does this mean that I can only specify a static function of a class?

With your SetFunction, yes.


> The more I think about this I can see why the compiler chokes on this.
> Aside from providing discete functions to wrap the call to
> bg.SetNewMode() is there any way to use a function pointer with a
> non-static function of a class?

Make your SetFunction take a pointer to an event interface:

struct ClickHandler
{
virtual void onClicked() = 0;
};

void Button::set( ClickHandler* handler )
{
myClickHandler = handler;
}

If you want to do general event handling check out Boost slots.


Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!

blargg

unread,
Jun 16, 2009, 3:18:40 PM6/16/09
to
lil...@dcccd.edu wrote:
[...]
> void BTN::SetFunction(int (*fnc)(void))
[...]

> However, the function I want
> to set it to is a public member of another class object, bg of class
> Grid.
>
> btnNew.SetFunction ( bg.SetNewMode );
>
> But I get a repremand to
>
> "use '&Grid::SetNewMode' to create a pointer to member"

bg.SetNewMode is invalid if SetNewMode is a non-static member function of
the class; as the compiler says, use &Grid::SetNewMode to form a member
function pointer.

> Does this mean that I can only specify a static function of a class?

Yes, using the syntax you want (object.static_member_function).

> The more I think about this I can see why the compiler chokes on this.
> Aside from providing discete functions to wrap the call to
> bg.SetNewMode() is there any way to use a function pointer with a
> non-static function of a class?

Member function pointers are covered in the C++ FAQ:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Jonathan Lee

unread,
Jun 16, 2009, 3:19:45 PM6/16/09
to
> Aside from providing discete functions to wrap the call to
> bg.SetNewMode() is there any way to use a function pointer with a
> non-static function of a class?

The way I see it, either bg.SetNewMode() does something statically,
or it doesn't. In the former case, you're in the clear. In the latter,
why don't you just pass the object? i.e., instead of setFunction()
use "setTarget()":

void BTN::setTarget(Grid* tar) { p_grid = tar; }

int BTN::func() {
if (!p_grid) throw HissyFit();

return p_grid->SetNewMode();
}

//...

btnNew.setTarget(&bg);

--Jonathan

lil...@dcccd.edu

unread,
Jun 17, 2009, 12:20:07 PM6/17/09
to
Thanks to everyone who post a response. I have a bit to chew on here
and I'm sure it will all be helpful.

I was probably remiss in giving all the details and I'm also probably
a victim of my own attempt to be clever. I'm working within an API
that disconnects me from the standard Windows events in that it's up
to me to poll the status of the mouse and keyboard through the API's
functions.

In the base class for the controls I'm trying to build I'm maintaing a
vector of pointers to the controls with the intent of calling a
check() function against all of them periodically that will trigger
the appropriate function assigned to the control. Apparently this
isn't going to be posible where the functions reside in class objects
since the function wouldn't have a "this" to relate to. I may have to
work out my own event system and work off of that.

Thanks again for your suggestions.

--
Lilith

0 new messages