I am trying to implement a callback mechanism based on gotw #83:
http://gotw.ca/gotw/083.htm

---------------------
class CallbackBase
{
public:
virtual void operator()(const TCHAR*) const { };
virtual ~CallbackBase() = 0;
};

template<typename T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)(const TCHAR*);

Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()(const TCHAR*) const { (t_->*f_)(const TCHAR*); }

private:
T* t_;
F f_;
};
-------------------------

I've been staring at it for a while now and I can't figure out why the line:

void operator()(const TCHAR*) const { (t_->*f_)(const TCHAR*); }

gives the following errors:

error C2143: syntax error : missing ')' before 'const'
error C2198: 'const Callback<T>::F' : too few actual parameters
error C2059: syntax error : ')'

Re: operator() by John

John
Sun Feb 08 04:40:30 CST 2004


"John" <john@no_spam> wrote in message
news:ubaVL9h7DHA.2524@TK2MSFTNGP11.phx.gbl...
> I am trying to implement a callback mechanism based on gotw #83:
> http://gotw.ca/gotw/083.htm
>
> ---------------------
> class CallbackBase
> {
> public:
> virtual void operator()(const TCHAR*) const { };
> virtual ~CallbackBase() = 0;
> };
>
> template<typename T>
> class Callback : public CallbackBase
> {
> public:
> typedef void (T::*F)(const TCHAR*);
>
> Callback( T& t, F f ) : t_(&t), f_(f) { }
> void operator()(const TCHAR*) const { (t_->*f_)(const TCHAR*); }
>
> private:
> T* t_;
> F f_;
> };
> -------------------------
>
> I've been staring at it for a while now and I can't figure out why the
line:
>
> void operator()(const TCHAR*) const { (t_->*f_)(const TCHAR*); }
>
> gives the following errors:
>
> error C2143: syntax error : missing ')' before 'const'
> error C2198: 'const Callback<T>::F' : too few actual parameters
> error C2059: syntax error : ')'


(t_->*f_)(const TCHAR*);

is meant to be a function call, not a function declaration. To make a
function call for a function that takes an argument, you of course need an
argument. Try:

void operator()(const TCHAR* tstr) const { (t_->*f_)(tstr); }


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)