delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/10/07:15:29

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How would I build a pointer to a method of my class
Date: 10 Apr 2001 11:09:41 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 50
Message-ID: <9aupll$5de$1@nets3.rz.RWTH-Aachen.DE>
References: <9apab3$mo7$1 AT bird DOT wu-wien DOT ac DOT at>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 986900981 5550 137.226.32.75 (10 Apr 2001 11:09:41 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 10 Apr 2001 11:09:41 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019