delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/18/02:31:21

From: Matthew Mastracci <mmastrac AT acs DOT ucalgary DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: An OOP question - pointer to member function
Date: Fri, 17 Apr 1998 18:54:24 -0600
Organization: The University of Calgary
Lines: 55
Message-ID: <6h8tjt$i8m@ds2.acs.ucalgary.ca>
References: <3536C543 DOT 70D1EF9A AT ms1 DOT accmail DOT com DOT tw> <ErKyCI DOT 5q1 AT world DOT std DOT com>
Reply-To: Matthew Mastracci <mmastrac AT acs DOT ucalgary DOT ca>
NNTP-Posting-Host: mmastrac AT acs2 DOT acs DOT ucalgary DOT ca
Mime-Version: 1.0
In-Reply-To: <ErKyCI.5q1@world.std.com>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Fri, 17 Apr 1998 tob AT world DOT std DOT com wrote:

> Also, HERO::action is of the wrong type to assign to member_func_ptr,
> (void (HERO::*) (void)). Making "action" a virtual function in "BASE"
> should do what you want.

Making it a (void (BASE::*)(void)) will ensure that you can take any
member function from any derived class that satisfies the void(void) 
prototype.  For the person who originally made the post, here is some
useful info: 

 - You are pointing to the RAW CLASS MEMBER FUNCTION.  This is the
function that is not "tied" to any specific object.  You must provide the
"this" pointer to the function by using the .* or ->* operators, ie:

BASE foo
HERO bar

// Take the pointer to the raw HERO::action function
foo.member_func_ptr = &HERO::action;

// Call foo.member_func_ptr as if it were a member of class HERO (which it
// just happens to actually be in this case!).
bar.*(foo.member_func_ptr)();

or

HERO * bar = new HERO;

bar->*(foo.member_func_ptr)();

(I'm pretty sure this is the proper syntax -- I can't find any actual
documentation on these operators, so I'm going from memory)

 - You can call a member function of a related class (ie: parent class)
using this syntax.  If class BASE had a function called base_action, you
could still do:

foo.member_func_ptr = &BASE::base_action;
bar.*(foo.member_func_ptr)();

 - This is one of the least useful things you can do in C++.  Anything you
can do with the .* operator, you can do much better with virtual
inheritance and special classes.  It's a poorly documented feature and it
makes some code unreadable (except to yourself), so I recommend getting a
good book on inheritance and working through it before getting used to
this method. 

/\/\att /\/\astracci                  mmastrac AT acs DOT ucalgary DOT ca

"Tout choses sont dites deja, mais comme personne n'ecoute, il faut
toujours recommencer."



- Raw text -


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