delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/23/17:45:36

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointers to [member] functions
Date: Sat, 23 May 1998 14:15:58 -0700
Organization: Alcyone Systems
Lines: 104
Message-ID: <35673C8E.2506756F@alcyone.com>
References: <3565FDC0 DOT 98FEB308 AT easynet DOT co DOT uk>
NNTP-Posting-Host: charmaine.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mr. Meanie wrote:

> I wish to make the pointer 'point' to class member functions.
> Here is an example class definition :
> 
> class TTest
> {
>  public:
>     typedef void (TTest::*TESTFUNCTIONPOINTER)();
> 
>     TESTFUNCTIONPOINTER fn;
> 
>     void Function();
> };
> 
> if fn is assigned a pointer to Function() by using
> 
> fn = Function;            //This would be found in a member function of
> TTest
> 
> GCC/G++ gives the warning "assuming & on TTest::Function()"
> When the same code is built under MSVC, no such problem occurs.

The proper syntax for assigning a member function pointer is to include
the class name with the scope resolution operator, and the address-of
operator:

    void (TTest::*fn)(void) fn = &TTest::Function;

or with your typedef:

    typedef void (TTest::*TESTFUNCTIONPOINTER)(void);

    TESTFUNCTIONPOINTER fn = &TTest::Function;

> And when the function is executed using
> 
> fn();            //This would be found in a member function of TTest
> 
> The error message "must use .* or ->* to call pointer-to-member
> function

Member function pointers are distinct from function pointers because it
becomes important which class instance is being called.  Just as you
can't call a member function without explicitly stating which instance
it belongs to, you can't call a member function pointer without
including the class instance.

So, for instance:

    TTest test; // constructed in some appropriate way

    (test->*fn)(); // call the member function pointer fn for instance 
                   // test

If you don't want the member function pointer to be tied to a class
instance, then you should declare the function which is being pointed to
static, and then it's a normal function pointer again.

Here's a complete program which illustrates how to use member function
pointers:

. 

#include <iostream.h>

class C
{
  public:
    int f(void) { cout << "in C::f" << endl; return 1; }
};

int main(void)
{
    int (C::*mfp)(void) = &C::f; // declare the member function pointer

    C *c = new C(); // get an instance of class C

    int r = (c->*mfp)(); // call the member function pointer through 
                         // that instance

    cout << "c->*mfp returned " << r << endl; // 

    return 0;
}

. 

Running this program should print:

in C::f
c->*mfp returned 1

Note you can also use the .* operator in a similar way to the ->*
operator if you have an instance (or a reference to an instance), rather
than a pointer to the instance.

-- 
         Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                       Alcyone Systems / http://www.alcyone.com/max/
  San Jose, California, United States / icbm:+37.20.07/-121.53.38
                                     \
   "Since when can wounded eyes see / If we weren't who we were"
                                   / Joi

- Raw text -


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