• Advertisement

Archived

This topic is now archived and is closed to further replies.

Callbacks in classes

This topic is 6010 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 there! I''m using a callback for a progressloader(for some dataloading routines), i define it by ''void call(void(*callback)(int));'' and I have it in a class. This works great for functions not part of a class, but when i try to use callbacks which are a part of a class, it just won''t work. MSVC error: error C2664: ''cb'' : cannot convert parameter 1 from ''void (int)'' to ''void (__cdecl *)(int)'' None of the functions with this name in scope match the target type I guess I have to declare the callback in another way, not sure how.. anyone ? :D kode : class callb { public: void cb(void(*callback)(int)) {} }; class back : callb { public: back() { cb(step); } void step(int) {} }; void main() { } kode end Thanks! :-)

Share this post


Link to post
Share on other sites
Advertisement
Two solutions: Make the callback member function static (i.e. it is no longer taking a this pointer in ecx), or make the function accept an entire "callback class" (which you could derive other classes from, use a pure virtual function for the callback, or whatever) and call the class''s function.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
you could make a friend function to execute the callback

  


exec g_pExec;


inline void func() { g_pExec->Function(); }


class exec()
{
friend func();

private:
void Function();

}


its messy a bit, but its kinda useful...

{ Stating the obvious never helped any situation !! }

Share this post


Link to post
Share on other sites
Observer method:

  
IEventNotification
{
virtual SomethingHappened()=0;
};

CEngineClass::AddObserver(IEventNotification* pEventSink)
{
m_pEventSink = pEventSink;
}

CEngineClass::SomeMethod
{
if(m_pEventSink)
m_pEventSink->SomethingHappened();
}



class CMyUIClass : public IEventNotification
{
virtual SomethingHappened()
{
MessageBox("Blah", "Something Happened", MB_OK);
}

Init()
{
m_pEngine = new CEngineClass;
m_pEngine->AddObserver(this);
}
}


The COM methology is called connection points...

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites

  • Advertisement