View Single Post
  #1 (permalink)  
Old October 20th, 2004, 04:28 PM
jacob jacob is offline
Friend of Wrox
 
Join Date: Jun 2003
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