473,499 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I use a function pointer to non-static member function of a class.

Hi All,

Except function pointer to a static member funciton of a class (or a
function).

As the following code, I could pointer a non-static member function of
a class.
And "Equal" was printed, the pointer has meaning.

But how can I use it??
I.e., I'd like to call the member method by using the function pointer
(like using c function pointer).
class TMyClass
{
public:
int DoIt(float a, char b, char c){ ... };
}

int (TMyClass::*pt2Member)(float, char, char) = NULL;
pt2Member = &TMyClass::DoIt;

// C++
if(pt2Member== &TMyClass::DoIt)
cout << "Equal" << endl;

pt2Member(1.2f, 'a', 'b'); //<------ error.

As u know, in case that the function pointer piont a non-static member
function, the pointer can invoke the member function.

Mar 9 '06 #1
1 2548
Assertor wrote:
[..]
As the following code, I could pointer a non-static member function of
a class.
And "Equal" was printed, the pointer has meaning.

But how can I use it??
You need an instance of the class in the form of an object, a reference,
or a pointer.
I.e., I'd like to call the member method by using the function pointer
(like using c function pointer).
class TMyClass
{
public:
int DoIt(float a, char b, char c){ ... };
} ;

int (TMyClass::*pt2Member)(float, char, char) = NULL;
pt2Member = &TMyClass::DoIt;
I suppose this is inside a function of some kind.

// C++
if(pt2Member== &TMyClass::DoIt)
cout << "Equal" << endl;

pt2Member(1.2f, 'a', 'b'); //<------ error.
You need an instance of 'TMyClass', and then use it:

TMyClass t;

(t.*pt2Member)(1.2f, 'a', 'b');
As u know, in case that the function pointer piont a non-static member
function, the pointer can invoke the member function.


Huh?

V
--
Please remove capital As from my address when replying by mail
Mar 9 '06 #2

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

Similar topics

10
2034
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
1823
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
1923
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
12883
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
2694
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
407
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
2756
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
2251
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
3873
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
375
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
7132
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
7009
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
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
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
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.