Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Pointer to ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 7 Topic 45807 of 47034
Post > Topic >>

Re: Pointer to non-static member functions

by peter koch <peter.koch.larsen@[EMAIL PROTECTED] > May 8, 2008 at 02:56 AM

On 8 Maj, 11:26, Tim Frink <plfr...@[EMAIL PROTECTED]
> wrote:
> Hi,
>
> I'm experimenting with function pointers and found
> two questions. Let's assume this code:
>
> =A0 1 #include <iostream>
> =A0 2 class A;
> =A0 3
> =A0 4 ////////////////////////////////////////////
> =A0 5 class B
> =A0 6 {
> =A0 7 =A0 public:
> =A0 8 =A0 =A0 void printB( void (A::*)(void) );
> =A0 9 };
> =A010
> =A011 void B::printB( void (A::*func)(void) )
> =A012 {
> =A013 =A0 *func(); // not working
> =A014 }
> =A015
> =A016 ////////////////////////////////////////////
> =A017 class A
> =A018 {
> =A019 =A0 public:
> =A020 =A0 =A0 void printA(void);
> =A021 =A0 =A0 void invokeB(void);
> =A022
> =A023 =A0 private:
> =A024 =A0 =A0 B myB;
> =A025 };
> =A026
> =A027 void A::printA(void)
> =A028 {
> =A029 =A0 std::cout << "A::print" << std::endl;
> =A030 =A0 return;
> =A031 }
> =A032
> =A033 void A::invokeB(void)
> =A034 {
> =A035 =A0 myB.printB( &A::printA );
> =A036 =A0 return;
> =A037 }
> =A038
> =A039 ////////////////////////////////////////////
> =A040 int main(void)
> =A041 {
> =A042 =A0 A myA;
> =A043 =A0 a.invokeB();
> =A044
> =A045 =A0 return 0;
> =A046 }
>
> What I want to achieve is a communication between two object
> with a callback function. Object A invokes a function in
> object B passing a callback function. The invoked function in
> object B calls the passed callback function to communicate
> with the caller A.
>
> So, I'm passing a pointer to A::printA in line 35 to
> B::printB. The called member function printB of object
> myB than should deference the passed function pointer
> (line 13, not working yet) to invoke again a function
> (A::printfA) of the original caller. Thus, myA invokes
> myB which in turn invokes myA again.
>
> My first question concerns line 35. What exactly is the
> address of &A::printA? When I forward this value to "cout",
> (cout << &A::printA) I get the output "1" and not an
> address.

Probably you did print "true" because the memberfunction is not null.
Apart from that, I do not see what interest you could have in getting
its adress. If it is out of curiosity, I would recommend you to
examine the value in a debugger instead.

> Since A::printA is a non-static member function,
> it cannot be considered independent of a concrete object.

Yes it can. A memberfunction is not connected to any concrete object.
I will come back to that later.

> Thus, I would assume that &AA::printA is an offset which
> must be added to the memory address where a concrete
> object of class A is allocated. Adding the start address
> and the offset would result in the address where the
> function printA of an individual object of class A is
> located.

I am not sure I can decipher the above, but if your understanding is
that a memberfunction somehow is placed inside an object, your
understanding is wrong. A memberfunction is does not take up any space
in an object.
>
> And this brings me to my second question concerning line
> 13. When I try to compiler this code, I get the compiler error:
> error: must use .* or ->* to call pointer-to-member function in `func
(...=
)'

(repeating offensive code)
>  11 void B::printB( void (A::*func)(void) )
>  12 {
>  13   *func(); // not working
>  14 }
>  15

This is because your perception of a member-function pointer is wrong.
A member-function pointer is a pointer that can be used to call a
memberfunction oon all objects of the given type. But you have to
provide an object. So your code should have been somewhat like:

>  11 void B::printB( A *a, void (A::*func)(void) )
>  12 {
>  13   (a->*func)();
>  14 }
>  15


> I assume the problem is that the function pointer "func" has
> no reference to a concrete object of class A (myA in this case).
> So, the function cannot be invoked. How can I solve this?
> Do I have to pass the address of myA to B::printB?
> Something like "myB.printB( &A::printA, this );" in line 35
> and than use the pointer in "this" to invoke func?
Exactly.


/Peter
 




 7 Posts in Topic:
Pointer to non-static member functions
Tim Frink <plfriko@[EM  2008-05-08 09:26:37 
Re: Pointer to non-static member functions
peter koch <peter.koch  2008-05-08 02:56:31 
Re: Pointer to non-static member functions
James Kanze <james.kan  2008-05-10 07:26:11 
Re: Pointer to non-static member functions
Krice <paulkp@[EMAIL P  2008-05-08 03:31:19 
Re: Pointer to non-static member functions
Szabolcs Ferenczi <sza  2008-05-08 03:41:11 
Re: Pointer to non-static member functions
James Kanze <james.kan  2008-05-09 04:51:03 
Re: Pointer to non-static member functions
Tim Frink <plfriko@[EM  2008-05-09 17:56:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:56:51 CDT 2008.