Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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...
Code:
#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...
Code:
class Test
{

public:

    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...
Code:
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...

Jacob.
__________________
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
Default

Hi jacob,
the following link might help you,
http://www.functionx.com/cpp/Level2/Lesson07.htm


--------------------------------------------
Mehdi.:)
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
Default

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


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





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