views:

86

answers:

2

I'm not yet very familiar with these but maybe someone can shine light on this example.

Imagine I have class CFoo and it will have a function to add a handler and a function which is a function pointer.

So something like this:

class CFoo {

int *pointedFunc(int a, int b) = 0;

void setFunc(int *func(int a, int b))
{
    pointedFunc = func;
}
};

Given the above context, I want to know the proper way of doing this. I don't think I have done it properly. Also, how would I go about calling pointedFunc?

Thanks

+1  A: 

In your example, pointedFunc is a member function that returns an int *. To make it a function pointer, you need parens around pointedFunc, like:

int (*pointedFunc)( int a, int b );

A typedef might make it clearer:

class CFoo {
    CFoo() : pointedFunc( NULL ) {}
    typedef int (*funcType)(int, int);
    funcType pointedFunc;

    void setFunc( funcType f ) {
         pointedFunc = f;
    }
};

To call the function, you can use either pointedFunc( 1, 2 ) or (*pointedFunc)(1, 2). I tend to use the latter to make it clear that you are going through a function pointer, but either will work.

Graeme Perrow
No it's not fine, right now he has a member function named `pointedFunc` and the return type is `int*`.
Ben Voigt
alright thanks, I guess I lack self confidence :p
Milo
@Ben: You're right - thanks for the clarification. I've fixed my answer.
Graeme Perrow
+6  A: 

Right now you have a member function returning int *, not a pointer to a function returning int. A set of parenthesis would fix that.

int (*pointedFunc)(int a, int b);

void setFunc(int (*pfunc)(int a, int b))
{
    pointedFunc = pfunc;
}

Also, member variables get initialized in the constructor ctor-initializer-list, like

CFoo::CFoo() : pointedFunc(0) {}

not like you did. Your = 0 was actually a pure-specifier (and it won't work unless the member function is virtual), when you fix the pointer-return-type vs pointer-to-function issue you'll find that the compiler also complains about your attempt to initialize it.

Using a typedef as Graeme suggests is the easiest and best way to keep track of function-pointer types.

Ben Voigt
Oh thanks, I did not know it was only for virtual functions
Milo
It's not so much that it's only for virtual functions, as that `= 0` means something **completely different** (cue Monty Python) when it appears in a function declaration as when it appears in a data declaration. Actually, `int (*pointedFunc)(int a, int b) = 0;` would be perfectly OK as a local variable, which can be initialized, but member variables get initialized in the constructor and not in the declaration.
Ben Voigt
While you're certainly correct about setting a function itself = 0 being a pure-specifier, I'm under the impression you can set a function *pointer* to NULL with no issues, which I believe was the original intent.
Bryan
@Bryan: absolutely you can set it (the variable with function-pointer type) to NULL. But not in the declaration of a non-static member variable. Non-static member variables get initialized in a *ctor-initializer-list*.
Ben Voigt
Oh... I submitted my answer partly written so I could check what his class name was, I guess you saw that before I added the *ctor-initializer-list* example.
Ben Voigt
@Ben: Oh... duh. I knew that. Hah, thanks for keeping me straight.
Bryan