Mail Archives: djgpp/2001/04/10/07:15:29
Wormy <wormy AT technologist DOT com> wrote:
> -------------------------------------------
> typedef void (My_func)(short parameter);
> My_func Pointer_to_My_func;
> -------------------------------------------
> and then, later in my code (DOTHIS_func and DOTHAT_func are declared and
> defined somewhere)
> -------------------------------------------
> if (i == 0)
> Pointer_to_My_func = DOTHIS_func;
> else
> Pointer_to_My_func = DOTHAT_func;
> (*Pointer_to_My_func)(parameter_to_pass);
> ----------------------------------------------
> OK.... but now, how am I doing it in C++ if I'm fumbling around with a
> class?
The "typical" OO-style method would be to not use a function pointer,
but rather a special-purpose class. I.e. you'ld set up a purely
abstract base class (don't sue me if the syntax is a bit off...):
class my_handlertye {
void handlermethod(short parameter) = 0;
}
Every handler function would be turned into a class derived from
my_handlertype, and then implement handlermethod(). Code using the
function pointer would instead use a pointer to an object of type
my_handlertype, and call handlermethod where needed:
my_handlertype * handler;
if (condition)
handler = new handler_type_1;
else
handler = new handler_type_0;
/* ... */
handler->handlermethod(argument);
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -