473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer to non-static member function in the same class

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


Jul 22 '05 #1
2 1201

"Lars Munch" <la**@segv.dk> wrote in message
news:pa***************************@segv.dk...
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
Jul 22 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote:

"Lars Munch" <la**@segv.dk> wrote in message
news:pa***************************@segv.dk...
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


That did the trick. Thank you so much.

:-) Lars

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
6
by: amparikh | last post by:
I know this is something fundamental and I ought to have known it, but somehow this seems to be confusing me a lot. Fundamentally, rvalues and/or temporaries can be bound only to constant...
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
10
by: Michael | last post by:
Hi, I'm trying to get my head around the relationship between pointers and arrays. If I have the following: int even = {2,4,6,8,10}; int *evenPtr = {even+4, even+3, even+2, even+1, even};...
1
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read...
2
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
1
by: Carmen Sei | last post by:
to delcare an object with pointer - MyCar * mycar = new MyCar; mycar->Create(); ======================== to delcare an object without pointer - MyCar mycar = new MyCar; mycar.Create(); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.