Callbacks in C++

M

makuchaku

Hi,

How can I implement callback mechanism in C++.

Basically I need it to provide exception handling to a user who will be
using a library developed by me?

If not callbacks, how can i enable an end user/developer to call his
custom functions in case of an error.

Please elaborate a bit... thanks alot,
makuchaku
 
N

Nicolas Pavlidis

makuchaku said:
Hi,

How can I implement callback mechanism in C++.

Basically I need it to provide exception handling to a user who will be
using a library developed by me?

Why you don't want to throw an exception to the user? He can do
something with it.
If not callbacks, how can i enable an end user/developer to call his
custom functions in case of an error.

There are several ways to do this:
1) The good old functionpointers.
2) Functionobjects (these are classes that define the operator () ()
3) The template way:
template<typename Callable>
void registerCallback(Callable const &call_back);
4) The Functors of the Lokilibrary.

But IMHO it is the best way to throw an exception, and let the user do
what he wants do do.
Please elaborate a bit... thanks alot,

HTH && Kind regards,
Nicolas
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,

How can I implement callback mechanism in C++.

Basically I need it to provide exception handling to a user who will be
using a library developed by me?

If not callbacks, how can i enable an end user/developer to call his
custom functions in case of an error.
Hmm... Im not sure there's enough of a fact pattern here to diagnose a
problem, however, if you are looking for a OO callback pattern, then the
observer pattern is what you are looking for. For a very good example,
see Bruce Eckel's "Thinking in C++, 2nd edition" online version at:
http://www.camtp.uni-mb.si/books/Thinking-in-C++/Chapter10.html#Heading323

Enjoy,
Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFB2fhxpxCQXwV2bJARAnc+AKDGFExc3ARVn+GLA1sS9vcX73iwtwCfXqpR
sqyS8Cwlsn9G0CbOZY5IEVo=
=USFq
-----END PGP SIGNATURE-----
 
M

makuchaku

Hi,
Thanks.

I'm already using exceptions & letting the user handle them. But i also
want user to override a method (say onError()) from my Error class,
which shall be called in case of exception_object.onError()

But the problem is how do i implement this mechanism using virtual
functions (user overriding the onError()), but still retaining the
arguments which i passed in my -- throw excepti_object("error string",
code, value)

Have a look at this code


//The Parent Error class from the Library
//SockErr.h
using namespace std;
class SockErr
{

private:

string message;


public:

SockErr()
{
}

SockErr(string message)
{
this->message=message;
}

string getMessage()
{
return(message);
}

virtual void onError()
{

}


};





//The class in error.cc which is written by the end-user
#include "SocErr.h"

using namespace std;
class SocketHandler : public SockErr
{
public:

void onError()
{
cout<<"Inside onError() of UserDefined
handler"<<endl;
}
};


int main(void)
{

try
{
Socket(10000, SOCK_STREAM, 0);
}
catch(SockErr *s)
{
s=new SocketHandler();
s->onError();

cout<<s->getMessage()<<endl<<s->getMessage().length()<<endl;
}
}


I need to use SockErr *s as i have to implement virtual functions....

so? any advice?

thanks,
makuchaku
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top