delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/22/19:01:39

From: "Mr. Meanie" <mrmeanie AT easynet DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Pointers to functions
Date: Fri, 22 May 1998 23:35:44 +0100
Organization: [posted via] Easynet UK
Lines: 118
Message-ID: <3565FDC0.98FEB308@easynet.co.uk>
NNTP-Posting-Host: mrmeanie.easynet.co.uk
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi,
    I am trying to use pointers to functions.

I define the following

typedef void (*FUNCTIONPOINTER)(void);

and If there is a function :

void Function();

then

FUNCTIONPOINTER fn = Function;
fn();

will work fine; it assigns a pointer to Function() to fn, and then
executes Function() via fn.

This works fine, but here is what I want to do.

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.

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

in
'TTest::WhatEverTheMemberFunctionIs()'"
is given.

Can you tell me what I am doing wrong.

Yours faithfully

-Geoffrey French



P.S. Here is a copy of the actual source I am using. The comments are
the error messages given.



#include <stdio.h>

class TFunctionTest
{
 public:
  typedef void (TFunctionTest::*TESTFUNCTION) ();

   TESTFUNCTION fn;
   long memberData;

 TFunctionTest();

   void Function();
   void Run();
};

TFunctionTest::TFunctionTest()
{
   memberData = 0;
   fn = Function;        //warning : assuming & on
'TFunctionTest::Function()'
}

void TFunctionTest::Function()
{
  memberData = 1;
}

void TFunctionTest::Run()
{
   fn();        //error : must use .* or ->* to call pointer-to-member
function in 'TTestFunction::Run()'
   printf("\nmemberData = %d\n", memberData);
   return;
}



int main()
{
   TFunctionTest *test;
   test = new TFunctionTest();
   test->Run();
   getchar();
   return 0;
}







- Raw text -


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