473,398 Members | 2,404 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,398 software developers and data experts.

Passing a member function as a parameter of a member function

Hi,

is it possible to pass a member function pointer to other member
function of the same class in order to get different behaviours?
Something like

class A
{

int m(int d) /* First way */
{
return d;
}

int n(int d) /*Second way*/
{
return d+2;
}

/* This does the calculations depending on the function passed */

void f(int (*g)(int))
{
std::cout << g(8) << "\n";
}

void start()
{
f(&m); /* Do it the "m" way */
f(&n); /* Do it the "n" way */
}
};

What I am trying to accomplish is to have a member function that can
call different procedures to do slightly different jobs.

Thank you,

Mar 6 '07 #1
2 4557
Azdo a écrit :
Hi,

is it possible to pass a member function pointer to other member
function of the same class in order to get different behaviours?
Something like

class A
{
int m(int d) /* First way */
[snip]
int n(int d) /*Second way*/
[snip]

/* This does the calculations depending on the function passed */

void f(int (*g)(int)
[snip]
void start()
{
f(&m); /* Do it the "m" way */
f(&n); /* Do it the "n" way */
}
};

What I am trying to accomplish is to have a member function that can
call different procedures to do slightly different jobs.
The declation of f() would take a pointer on A's member function:
void f(int (A::*g)(int));
{
std::cout << this->*g(8) << "\n";
}

and
f(&A::m);
f(&A::n);
Michael
Mar 6 '07 #2
On 6 mar, 12:13, Michael DOUBEZ <michael.dou...@free.frwrote:
Azdo a écrit :
Hi,
is it possible to pass a member function pointer to other member
function of the same class in order to get different behaviours?
Something like
class A
{
int m(int d) /* First way */
[snip]
int n(int d) /*Second way*/
[snip]
/* This does the calculations depending on the function passed */
void f(int (*g)(int)
[snip]
void start()
{
f(&m); /* Do it the "m" way */
f(&n); /* Do it the "n" way */
}
};
What I am trying to accomplish is to have a member function that can
call different procedures to do slightly different jobs.

The declation of f() would take a pointer on A's member function:
void f(int (A::*g)(int));
{
std::cout << this->*g(8) << "\n";

}

and
f(&A::m);
f(&A::n);

Michael
Great! I had however to do a tiny change for that to work:

std::cout << (this->*g)(8) << "\n";

Thank you very much Michael.

Mar 6 '07 #3

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
5
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: ifitzgerald | last post by:
I am attempting to use a wrapper class to the Win32 timer API in a Visual C++ 6.0 MFC application. The callback function needs to have access to the front panel, and as such needs to be a member of...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.