Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 20th, 2004, 04:28 PM
Friend of Wrox
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default pointer-to-function in C++

Have got a problem wit pointer-to-funtion in C++, so here it goes...

I want to define some functions in some class A that I can give as arguments to a function from some other class B. However I cannot make this work. First I have made this small program that actually works...
#include <iostream>

using std::cout; 
using std::endl;

bool testMethod()
    return false;

bool test(bool (*method)())
    return (*method)();

int main()
    cout << test(testMethod) << endl;    

    return 0;
And then I moved on to doing something like this, which does not work...
class Test


    bool someMethod(void)
        return false;

    bool testMethod(bool (*method)(void))
        return (*method)();

    void test()
        bool t = this->testMethod(someMethod);

The error message I get when I do this is like the following...
error C2664: 'Test::testMethod' : cannot convert parameter 1 from 'bool (void)' to 'bool (__cdecl *)(void)'
Is it some kind of typecasting problem? And how to get around it? It is the same error message I get if I set up the two class that I want.

Thanks in advance...

Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
Reply With Quote
  #2 (permalink)  
Old October 22nd, 2004, 05:47 AM
Friend of Wrox
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post

Hi jacob,
the following link might help you,

Reply With Quote
  #3 (permalink)  
Old October 23rd, 2004, 05:20 AM
Authorized User
Join Date: Oct 2004
Location: San Francisco, CA, .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Jacob,

Pointers to methods are a little trickier than pointers to functions. They need to be referred to in the context of an object (or a class if it's a pointer to a static method). In your example, the correct code for the test() method is:

  bool testMethod(bool (Test::*methodPtr)(void))
    return (this->*methodPtr)();

Note that the parameter type refers to the method in the context of its class and the call uses an actual object. This syntax is dense, but it's flexible -- it means you can pass a method on a class and call it on any object of that class, not just on "this". Kind of neat.

To pass a method in a function or method, you also need to refer to it in the context of its class. So the correct code for Test::test() is:

void Test::test()
  bool t = this->testMethod(&Test::someMethod);

Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to struct goscho C++ Programming 1 January 10th, 2008 07:23 AM
Constant pointer and pointer to a constant amit_mande@yahoo.com C++ Programming 2 June 5th, 2007 01:39 AM
Pointer fReqZz C++ Programming 5 June 5th, 2007 01:09 AM
Function/Subroutine Pointer Parameter pjm Access VBA 3 October 3rd, 2006 09:07 AM
Pointer reference perstam C++ Programming 5 April 29th, 2006 08:51 AM

All times are GMT -4. The time now is 04:09 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.