Jump to content
  • Advertisement

Archived

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

raiden

Callbacks in classes

This topic is 6284 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
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!