Pointer to non-static member function in the same class

L

Lars Munch

Hello

I would like to create a pointer to non-static member function of a
class and use it in the same class. I have tried the following:

============ Example begin ================
#include <stdio.h>

class c;

typedef void (c::*FP)(void);

class c
{
FP fp;
public:
void test_fp();
void test();
};

void c::test_fp() {
printf("test_fp\n");
}

void c::test() {
fp = &c::test_fp;
(this->fp)();
}

int main() {
c tfp;
tfp.test();
return 0;
}

============ Example end ================

But with this I get:
test.cpp:27: error: call to non-function `c::fp'

What am I doing wrong?

Thanks a lot
Best regards

-- Lars Munch
 
J

John Harrison

Lars Munch said:
Hello

I would like to create a pointer to non-static member function of a
class and use it in the same class. I have tried the following:

============ Example begin ================
#include <stdio.h>

class c;

typedef void (c::*FP)(void);

class c
{
FP fp;
public:
void test_fp();
void test();
};

void c::test_fp() {
printf("test_fp\n");
}

void c::test() {
fp = &c::test_fp;
(this->fp)();

(this->*fp)();

Untested code.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top