472,328 Members | 976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 2434
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
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() {...
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...
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...
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>...
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...
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...
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...
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...
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++ =...
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 -...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.