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

3
  • 1
    Not "a function which is a function pointer", but "a member variable (known in OO as a field) which is a function pointer". Commented Oct 1, 2010 at 0:39
  • Please avoid function pointers in C++ code. Define an interface and create types that implement that interface. You can then store objects neatly without having to resort to C hocks-pockus of function pointers. Commented Oct 1, 2010 at 1:04
  • @Martin: Sometimes an interface is the right approach, sometimes templated (and duck-typed) functors, and sometimes function pointers are best. Especially for dynamic linking, function pointers are needed. Commented Oct 1, 2010 at 5:20

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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: 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.
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: Oh... duh. I knew that. Hah, thanks for keeping me straight.
3

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.

1 Comment

No it's not fine, right now he has a member function named pointedFunc and the return type is int*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.