T
Tim Frink
Hi,
I'm experimenting with function pointers and found
two questions. Let's assume this code:
1 #include <iostream>
2 class A;
3
4 ////////////////////////////////////////////
5 class B
6 {
7 public:
8 void printB( void (A::*)(void) );
9 };
10
11 void B:rintB( void (A::*func)(void) )
12 {
13 *func(); // not working
14 }
15
16 ////////////////////////////////////////////
17 class A
18 {
19 public:
20 void printA(void);
21 void invokeB(void);
22
23 private:
24 B myB;
25 };
26
27 void A:rintA(void)
28 {
29 std::cout << "A:rint" << std::endl;
30 return;
31 }
32
33 void A::invokeB(void)
34 {
35 myB.printB( &A:rintA );
36 return;
37 }
38
39 ////////////////////////////////////////////
40 int main(void)
41 {
42 A myA;
43 a.invokeB();
44
45 return 0;
46 }
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:rintA in line 35 to
B:rintB. 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:rintfA) 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:rintA? When I forward this value to "cout",
(cout << &A:rintA) I get the output "1" and not an
address. Since A:rintA is a non-static member function,
it cannot be considered independent of a concrete object.
Thus, I would assume that &AA:rintA 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.
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 (...)'
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:rintB?
Something like "myB.printB( &A:rintA, this );" in line 35
and than use the pointer in "this" to invoke func?
Thank you.
Regards,
Tim
I'm experimenting with function pointers and found
two questions. Let's assume this code:
1 #include <iostream>
2 class A;
3
4 ////////////////////////////////////////////
5 class B
6 {
7 public:
8 void printB( void (A::*)(void) );
9 };
10
11 void B:rintB( void (A::*func)(void) )
12 {
13 *func(); // not working
14 }
15
16 ////////////////////////////////////////////
17 class A
18 {
19 public:
20 void printA(void);
21 void invokeB(void);
22
23 private:
24 B myB;
25 };
26
27 void A:rintA(void)
28 {
29 std::cout << "A:rint" << std::endl;
30 return;
31 }
32
33 void A::invokeB(void)
34 {
35 myB.printB( &A:rintA );
36 return;
37 }
38
39 ////////////////////////////////////////////
40 int main(void)
41 {
42 A myA;
43 a.invokeB();
44
45 return 0;
46 }
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:rintA in line 35 to
B:rintB. 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:rintfA) 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:rintA? When I forward this value to "cout",
(cout << &A:rintA) I get the output "1" and not an
address. Since A:rintA is a non-static member function,
it cannot be considered independent of a concrete object.
Thus, I would assume that &AA:rintA 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.
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 (...)'
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:rintB?
Something like "myB.printB( &A:rintA, this );" in line 35
and than use the pointer in "this" to invoke func?
Thank you.
Regards,
Tim