Sign in to follow this  

Function pointer problem...

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi...check out my problem.. I have a X class that have this attribute:
void (*FOnMouseClick)(PInputEventReceiver* ASender);
and this method to set this attribute:
void OnMouseClick(void (*AOnMouseClick)(PInputEventReceiver* ASender));
now...I have a Y class that have an object of type X and I need to set the attribute...so I declare
private:
  void IncreaseButtonMouseClick(PInputEventReceiver* ASender);
and in the constructor:
FIncreaseButton->OnMouseClick(IncreaseButtonMouseClick);
but I get this error: error C2664: 'Predator::Drawing::PInputEventReceiver::OnMouseClick' : cannot convert parameter 1 from 'void (Predator::Drawing::PInputEventReceiver *)' to 'void (__cdecl *)(Predator::Drawing::PInputEventReceiver *)' None of the functions with this name in scope match the target type if I write the function out of my class and set to the object of type X it works = ( what I have to do????? any suggestions?? thanx

Share this post


Link to post
Share on other sites
This:

private:

void IncreaseButtonMouseClick(PInputEventReceiver* ASender);

is a member function, so you have to use pointer-to-member function semantics. Look here for details.

A cleaner solution can be obtained with Boost.Function and Boost.Bind:


// This will demonstrate callbacks to member functions in a more general case.

typedef boost::function<int (int)> Callback;

struct X {
int foo(int);
};

struct Y {
void setCallback(Callback cback);
};

X x;
Y y;

Callback cback = boost::bind<int>(&X::foo, boost::ref(x), _1);

y.setCallback(cback);





Note: this code is untested and may contain errors.

[Edited by - nilkn on January 28, 2006 10:34:41 PM]

Share this post


Link to post
Share on other sites
Hi...

firstly..thanx for the reply...

after read the articles and some others...

I conclude that C++ is very poor 'needy' in this way...the only workaround more elegant for my problem is this


static void IncreaseButtonMouseClick(PInputEventReceiver* AObject, PInputEventReceiver* ASender);


but, I CAN'T pass this function to my:


void OnMouseClick(void (*AOnMouseClick)(PInputEventReceiver* ASender));


I'll not put the SomeClass pointer in my functions just to resolve this problem...

how can I head off the 'this' parameter...obscure in the member functions?

I have not found any good and elegant way...

Thanx

Share this post


Link to post
Share on other sites
Quote:
I conclude that C++ is very poor 'needy' in this way...


The lack of native support for bound pointers to members is indeed irritating.

Quote:
how can I head off the 'this' parameter...obscure in the member functions?


The difference between pointers and pointers to members go beyond the presence of an implicit "this". Things such as virtual functions, multiple and virtual inheritance complicate the situation significantly.

A pointer to member is not a pointer, and likely never will be.

Quote:
but, I CAN'T pass this function to my:
void OnMouseClick(void (*AOnMouseClick)(PInputEventReceiver* ASender));


Then change it!

Quote:
Original post by Kerkhoff
I have not found any good and elegant way...


Use Boost. boost::function, boost::bind and boost::mem_fn do what you want.

Note that they still won't be pointers. They will be objects. But they should simplify your design. Just don't expect to be able to pass them to an existing callback system that expects real pointers (e.g. Win32)

Share this post


Link to post
Share on other sites
Ok...thank god my events has the basic Sender argument..

but...I'm with another problem,

when I cast my Sender to some object, I lost some data from the object..

like this..


void ButtonIncreaseMouseClick(PInputEventReceiver* ASender)
{
PNumericUpDown* MySelf = ((PNumericUpDown*)ASender);

if (MySelf->GetValue() < MySelf->GetMaxValue())
MySelf->SetValue(MySelf->GetValue() + 1);
}




the MySelf->GetValue() returns a crazy number...but..the caption of buttons is right = (

what is happening?

this is my constructor:


PNumericUpDown::PNumericUpDown(SRect* ABounds, PGraphicObject* AParent, IVideoDriver* AVideoDriver):PInputEventReceiver(ABounds, AParent, AVideoDriver)
{
FEdit = new PTextBox(new SRect(ABounds->Left(), ABounds->Top(), (ABounds->Width() * 0.9), ABounds->Height()), ET_NUMERIC, AParent, AVideoDriver);

FButtonIncrease = new PButton("+", new SRect(FEdit->GetBounds()->Width() + FEdit->GetBounds()->Left() + (ABounds->Width() * 0.025), ABounds->Top(), (ABounds->Width() * 0.075), (ABounds->Height() * 0.5)), RT_NORMAL, AParent, AVideoDriver);
FButtonDecrease = new PButton("-", new SRect(FButtonIncrease->GetBounds()->Left(), FButtonIncrease->GetBounds()->Height() + FButtonIncrease->GetBounds()->Top(), FButtonIncrease->GetBounds()->Width(), (ABounds->Height() * 0.5)), RT_NORMAL, AParent, AVideoDriver);

FEdit->SetReadOnly(true);

GetChildReceivers()->Insert(FButtonIncrease);
GetChildReceivers()->Insert(FButtonDecrease);
GetChildReceivers()->Insert(FEdit);

FValue = 0;
FMinValue = 0;
FMaxValue = 100;

FButtonIncrease->OnMouseClick(ButtonIncreaseMouseClick);
FButtonDecrease->OnMouseClick(ButtonDecreaseMouseClick);
}




thanx

Share this post


Link to post
Share on other sites
forgive me please...

I found my mistake...

the ASender argument is the PButton and don't the PNumericUpDown = (

now..I'll see how can I workaround this problem...

thanx

<=)

Share this post


Link to post
Share on other sites

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this