delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/25/08:30:21

Message-ID: <397D8764.BA29E1BF@networkusa.net>
From: Weiqi Gao <weiqigao AT networkusa DOT net>
Organization: Object Computing, Inc.
X-Mailer: Mozilla 4.72 [en] (X11; U; Linux 2.2.14-5.0 i586)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointer to a function in class?
References: <8li587$566$1 AT nnrp1 DOT deja DOT com>
Lines: 55
Date: Tue, 25 Jul 2000 07:26:12 -0500
NNTP-Posting-Host: 216.15.147.96
X-Trace: news1.i1.net 964527922 216.15.147.96 (Tue, 25 Jul 2000 07:25:22 CDT)
NNTP-Posting-Date: Tue, 25 Jul 2000 07:25:22 CDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

sami3079 AT my-deja DOT com wrote:
> 
> Hello,
> 
> How do I get a pointer to a function in class?
> 
> Here is how I've tried:
> 
> ----
> class myclass
> {
>     public:
> 
>     void myfunction();
>     void otherfunction();
> };
> 
> void myclass::myfunction()
> {
>     ...
> }
> 
> void myclass::otherfunction()
> {
>     void *p = this->*myfunction;
> }
> ----
> 
> If I try ''this->*myfunction'' it gives an error message,
> that overload not implemented. Does this mean I can't
> do this in djgpp?

Yes you can.  The details are on p.p.418--420 of The C++ Programming
Language:
  void myclass::otherfunction()
  {
    // Declare type
    typedef void (myclass::*pointertomemberfunction)();
    // Get the pointer
    pointertomemberfunction p = &myclass::myfunction;
    // Call the function through 'this'
    (this->*p)();
    // Make another instance of myclass
    myclass x = myclass();
    // Call the function through the instance
    (x.*p)();
    // Another one, this time a pointer
    myclass *y = new myclass();
    // Call the function through the instance
    (y->*p)();
  }

-- 
Weiqi Gao
weiqigao AT networkusa DOT net

- Raw text -


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