Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing a member function as a parameter of a member function

Reply
Thread Tools

Passing a member function as a parameter of a member function

 
 
Azdo
Guest
Posts: n/a
 
      03-06-2007
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( << "\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,

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      03-06-2007
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( << "\n";
}

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


Michael
 
Reply With Quote
 
 
 
 
Azdo
Guest
Posts: n/a
 
      03-06-2007
On 6 mar, 12:13, Michael DOUBEZ <(E-Mail Removed)> wrote:
> 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( << "\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)( << "\n";

Thank you very much Michael.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
how to passing member function as parameter sun C++ 2 03-11-2008 10:02 AM
Passing pointer to member function to different member function that then calls for_each pookiebearbottom@yahoo.com C++ 8 05-24-2005 01:50 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments