Function pointer to function inside class 
Author Message
 Function pointer to function inside class

If I have the following class:

class CA
{
        void foo() {cout << "foo" <<endl;}:

Quote:
};

How do I declare a function pointer to foo?

I tried this:

main()
{

        void (CA::*ptr)();
        ptr=CA::foo;
        ptr();

Quote:
}

and the first two lines compiled, but the third doesn't.

If I take foo out of class CA, then I can do this:

main()
{
        void (*ptr)();
        ptr=foo;
        ptr();

Quote:
}

and it works. Somehow, putting foo in the class screws things up. I
have a feeling it may have to do with the declaration of the function
pointer for the case where foo is in CA, i.e. the line

void (CA::*ptr)();

but this is the only way it will compile.

Any ideas?

Thanks very much.

Dave



Sun, 13 Jul 2003 03:41:08 GMT  
 Function pointer to function inside class
void (CA::*ptr)();
ptr=&CA::foo;
CA a;
(a.*ptr)();

--
With best wishes,
    Igor Tandetnik


Quote:
> If I have the following class:

> class CA
> {
> void foo() {cout << "foo" <<endl;}:
> };

> How do I declare a function pointer to foo?

> I tried this:

> main()
> {

> void (CA::*ptr)();
> ptr=CA::foo;
> ptr();
> }

> and the first two lines compiled, but the third doesn't.

> If I take foo out of class CA, then I can do this:

> main()
> {
> void (*ptr)();
> ptr=foo;
> ptr();
> }
> and it works. Somehow, putting foo in the class screws things up. I
> have a feeling it may have to do with the declaration of the function
> pointer for the case where foo is in CA, i.e. the line

> void (CA::*ptr)();

> but this is the only way it will compile.

> Any ideas?

> Thanks very much.

> Dave



Sun, 13 Jul 2003 04:41:44 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to use (pointer to function), and function and pointer to (pointer to function)

2. function pointer inside a class

3. Setting a pointer to a function, inside a function

4. Class function pointer does not evaluate to a function

5. function pointers to functions in classes.

6. how to let a global function pointer point to a function in a Class

7. Pointer Functions and Pointers to Pointer Functions

8. Question about signal()/pointers to functions that return pointers to functions

9. function pointers and function pointers array

10. C++ function pointers versus C function pointer problem

11. Pointer to function returning pointer to function returning...

12. Pointers-to-functions inside a struct

 

 
Powered by phpBB® Forum Software