delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/27/16:45:36

From: "Tom" <TomHey AT dial DOT pipex DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: 3 questions
Date: Tue, 27 Apr 1999 21:07:46 +0100
Organization: UUNET WorldCom server (post doesn't reflect views of UUNET WorldCom
Lines: 57
Message-ID: <7g55sa$p5s$1@lure.pipex.net>
References: <oo5V2.119790$Mb DOT 45033755 AT newscontent-02 DOT sprint DOT ca> <7g3ia8$fhm AT dfw-ixnews10 DOT ix DOT netcom DOT com> <3725EF51 DOT 99B5D049 AT unb DOT ca>
NNTP-Posting-Host: usero996.uk.uudial.com
X-Trace: lure.pipex.net 925244106 25788 193.149.90.250 (27 Apr 1999 20:15:06 GMT)
X-Complaints-To: abuse AT uk DOT uu DOT net
NNTP-Posting-Date: 27 Apr 1999 20:15:06 GMT
X-Newsreader: Microsoft Outlook Express 4.72.3110.1
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

>> >2) How do I make a pointer to a class function and call it using that
>> >pointer?
>> This is a C++ question, not a DJGPP question, but the answer is that you
>> can't take the pointer of a member function unless it is a static member
>> function.  See the following:
>
>    That is incorrect.  This compiles under DJGPP and shows how to take the
>address of a non-static member function and call it.  Please note that you
must
>explicitly pass the object to the function.  There are some warnings which
can
>be removed with casts, but this runs and prints what you would expect
"static
>\n myMember bb = 42".  And if you feel it is too much to separetly take
care of
>the function pointer and the object pointer, just stick them in a struct,
and
>make a #define which will call it correctly.


Although this works the operators ::*  and ->* allow pointers to member
functions and data. These operators will allow pointers to members to
function correctly with virtual functions. In concept these work like
offsets requiring a base pointer.

Example of member function pointers

#include <stdio.h>

class foo
{
  public:
     int bb;
     void myMember( void );
};

void foo::myMember()
{
printf("myMember: bb = %d", bb);
}

void main( void )
{
  void (*ptr)();

  // NB: ALL the brackets on the following line are required
  void (foo::*member_ptr)() = foo::myMember;

  foo bar = {42};
  foo* pbar = &bar;

  // NB: ALL the brackets on the following line are required
  (pbar->*member_ptr)();
}


- Raw text -


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