Re: Member function pointer woes
"Albert" <albert@iitg.er net.in> wrote in message
news:e9ced22e.0 406290508.5a913 be@posting.goog le.com...[color=blue]
> Hi,
>
> I need to pass a pointer-to-member-function as a parameter to a function
> which takes pointer-to-function as an argument. Is there any way to do it
> besides overloading the function?
>
> Here is a small program I wrote to check if I could get the address of the
> function from the member-function-pointer, so that I could pass it to the
> function as a normal function-pointer.
>
> ---------------------------------------------------
> #include<stdio. h>
>
> class classname{
> public:
> int add(int a, int b){
> printf("inside add. \ta = %d, b = %d\n", a,b);
> return a+b;
> }
> }obj;
>
>
> typedef int(classname:: *mfptr)(int, int);
> typedef int (*fptr)(int, int);
>
>
> main(){
>
> mfptr mem_func_ptr;
> fptr func_ptr;
>
> mem_func_ptr = &classname::add ;
> func_ptr = (fptr)(&classna me::add);
>
> printf("&classn ame::add is %llx\n", &classname::add );
> printf("&classn ame::add is %llx\n", &classname::add );
>
> printf("mem_fun c_ptr is %llx\n", mem_func_ptr);
> printf("func_pt r is %x\n\n", func_ptr);
>
> printf("add called with func_ptr. sum is %d\n\n", func_ptr(2,3));
> printf("add called with mem_func_fptr. sum is %d\n\n",
> (obj.*mem_func_ ptr)(2,3));
> }
>
> -----------------------------------------------------
>
>
> I compiled it with the -Wno-pmf-conversions option. Here is the output of
> the program:
>
> ------------------------------------------
> &classname:: add is ffbeee78ff23e5d 0
> &classname:: add is ffbeee78ff243a4 c
>
> mem_func_ptr is ffbeee78ff243a4 c
> func_ptr is 109bc
>
> inside add. a = 3, b = 68028
> add called with func_ptr. sum is 68031
>
> inside add. a = 2, b = 3
> add called with mem_func_fptr. sum is 5
>
> ------------------------------------------
>
>
> A few questions:
>
> 1. Why is the value of '&classname::ad d' in the two lines different?[/color]
If the code you show is actually the one running, I find this very strange
since
as far as I can tell, the codelines for your 2 print-statements are
identical.
[color=blue]
> 2. Why does the value of parameter 'b' change when 'classname::add ' is
> called with 'func_ptr'?[/color]
I believe all memberfunctions have a hidden 'this' parameter. When you're
calling
a memberfunction as a regular function, the 'this' parameter is not added to
the stack
and you will get undefined behaviour.
[color=blue]
> 3. If this approach is not correct, then could you please advise as to how
> to go about with it?[/color]
It's hard to make recommodations withouth knowing much about how it is going
to be used,
but I think I would stay with either just regular function-pointers or just
memberfunction-pointers.
You can do much fun stuff with memberfunction-pointers, like passing
different virtual memberfunctions
as parameters to other memberfunctions .
PKH
"Albert" <albert@iitg.er net.in> wrote in message
news:e9ced22e.0 406290508.5a913 be@posting.goog le.com...[color=blue]
> Hi,
>
> I need to pass a pointer-to-member-function as a parameter to a function
> which takes pointer-to-function as an argument. Is there any way to do it
> besides overloading the function?
>
> Here is a small program I wrote to check if I could get the address of the
> function from the member-function-pointer, so that I could pass it to the
> function as a normal function-pointer.
>
> ---------------------------------------------------
> #include<stdio. h>
>
> class classname{
> public:
> int add(int a, int b){
> printf("inside add. \ta = %d, b = %d\n", a,b);
> return a+b;
> }
> }obj;
>
>
> typedef int(classname:: *mfptr)(int, int);
> typedef int (*fptr)(int, int);
>
>
> main(){
>
> mfptr mem_func_ptr;
> fptr func_ptr;
>
> mem_func_ptr = &classname::add ;
> func_ptr = (fptr)(&classna me::add);
>
> printf("&classn ame::add is %llx\n", &classname::add );
> printf("&classn ame::add is %llx\n", &classname::add );
>
> printf("mem_fun c_ptr is %llx\n", mem_func_ptr);
> printf("func_pt r is %x\n\n", func_ptr);
>
> printf("add called with func_ptr. sum is %d\n\n", func_ptr(2,3));
> printf("add called with mem_func_fptr. sum is %d\n\n",
> (obj.*mem_func_ ptr)(2,3));
> }
>
> -----------------------------------------------------
>
>
> I compiled it with the -Wno-pmf-conversions option. Here is the output of
> the program:
>
> ------------------------------------------
> &classname:: add is ffbeee78ff23e5d 0
> &classname:: add is ffbeee78ff243a4 c
>
> mem_func_ptr is ffbeee78ff243a4 c
> func_ptr is 109bc
>
> inside add. a = 3, b = 68028
> add called with func_ptr. sum is 68031
>
> inside add. a = 2, b = 3
> add called with mem_func_fptr. sum is 5
>
> ------------------------------------------
>
>
> A few questions:
>
> 1. Why is the value of '&classname::ad d' in the two lines different?[/color]
If the code you show is actually the one running, I find this very strange
since
as far as I can tell, the codelines for your 2 print-statements are
identical.
[color=blue]
> 2. Why does the value of parameter 'b' change when 'classname::add ' is
> called with 'func_ptr'?[/color]
I believe all memberfunctions have a hidden 'this' parameter. When you're
calling
a memberfunction as a regular function, the 'this' parameter is not added to
the stack
and you will get undefined behaviour.
[color=blue]
> 3. If this approach is not correct, then could you please advise as to how
> to go about with it?[/color]
It's hard to make recommodations withouth knowing much about how it is going
to be used,
but I think I would stay with either just regular function-pointers or just
memberfunction-pointers.
You can do much fun stuff with memberfunction-pointers, like passing
different virtual memberfunctions
as parameters to other memberfunctions .
PKH
Comment