www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/08/06:45:28

From: hhkraemer AT web DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: How would I build a pointer to a method of my class
Date: Sun, 08 Apr 2001 10:38:43 GMT
Lines: 94
Message-ID: <3ad03a62.57917390@news.cis.dfn.de>
References: <9apab3$mo7$1 AT bird DOT wu-wien DOT ac DOT at>
NNTP-Posting-Host: a0079.pppool.de (213.6.0.121)
X-Trace: fu-berlin.de 986726306 6531547 213.6.0.121 (16 [27606])
X-Newsreader: Forte Free Agent 1.21/32.243
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sun, 8 Apr 2001 11:16:25 +0200, "Wormy" <wormy AT technologist DOT com>
wrote:

> Hello all!
> 
> In simple C, I would do something like this:
> 
> 
> -------------------------------------------
> 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?
> 
> ------------
> class CMy_class
> {
>     void DOTHIS_method(short parameter);
>     void DOTHAT_method(short parameter);
> 
>     void ANOTHER_method();
> };
> 
> CMy_class My_class;
> --------------------------------
> 
> And, in the method ANOTHER_method() I would like to set the pointer to one
> of the two methods above (as I did it in C)... BUT I DON'T HAVE ANY IDEA
> HOW! I wrote down a simple pseudocode below.
> (NOTE: DOTHIS_method and DOTHAT_method are all methods of the same class)
> 
> -------------------------------
> void ANOTHER_method()
> {
> .
> .
> .
> 
>     if (i == 1)
>         pointer should point to DOTHIS_method;
>      else
>         pointer should point to DOTHAT_method;
> 
>     and now call the method my pointer is pointing to
> 
> .
> .
> .
> }
> -----------------------------
> 
> Can someone please give me a hint? BTW I am using VISUAL C++ 6.0 if this
> problem couldn't be solved in "ANSI" C++


class CMy_class
{
   void DOTHIS_method(short parameter);
   void DOTHAT_method(short parameter);
   typedef void (CMy_class::*PMF) (short);
public: 
   void ANOTHER_method();
};

void CMy_class::ANOTHER_method(short)
{
  PMF pmf;

  pmf = &CMy_class::DOTHIS_method;

  (this->*pmf)(42);
}

This is ANSI C++ and supported by MSVC++ (and gcc of course).

 
Regards
Horst

- Raw text -


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