using function pointers inside classes...

Jul 19, 2011 at 3:55pm
So yeah, anyone know how to do this properly?

the code would look like:

1
2
3
4
5
6
7
class outer{
    function_that_calls_another_function(&testfunc);
}

testfunc(int i, void* v){
// stuff here
}
Jul 19, 2011 at 3:59pm
Jul 19, 2011 at 4:05pm
Thanks, but I already know what a function pointer is and how to use them when there in scope. But how do you use them when they are out of scope like in the sample code I posted?

You can't just toss the function in the class as that makes it dynamic and makes the pointers invalid.
Jul 19, 2011 at 4:11pm
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


class MyType
{
public:

    void set_callback(void(*func_ptr)(int, void*));
};

// ...

void testfunc(int i, void* v)
{
    // ...
}

int main()
{
    MyType mytype;

    mytype.set_callback(&testfunc);

    // ...
}
Last edited on Jul 19, 2011 at 4:12pm
Jul 19, 2011 at 4:16pm
yeah I thought about that but I'm trying to make it so that the user doesn't have to specify the function every time they use the class.
Jul 19, 2011 at 4:20pm
Then pass the function pointer to the class' constructor, save it in a variable and just call it whenever you need it.
Jul 19, 2011 at 4:26pm
How do you do that though if it's out of scope?
Jul 19, 2011 at 4:33pm
oh, you mean just do what galik said but store it in a variable.

you would still need to specify it once and you really shouldn't need too, it seems necessary.
Jul 19, 2011 at 4:34pm
You must ensure it doesn't go out of scope. Don't ask the butcher for apples!

If you want us to be more specific, then post code that is a bit more specific, because with your original code I see no issues. I can do:

1
2
3
4
5
6
int main()
{
    outer someObject;
    somObject.function_that_calls_another_function(&testfunc);
    return 0;
}


Since testfunc() is not a function inside any class, it produces a regular, good ol' and plain C function pointer. The function is always accessible.
Jul 19, 2011 at 4:35pm
You have to do that while it is still in scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class MyType
{
private:
    void(*callback)(int, void*);

public:

    // the function must be in scope when this is called
    void set_callback(void(*func_ptr)(int, void*))
    {
        callback = func_ptr;
    }

    // the original function does not have to be in scope when this is called
    // because its pointer is stored in the internal scope of the object.
    void do_stuff() 
    {
        callback(7, 0);
    }
};

// ...

void testfunc(int i, void* v)
{
    // ...
}

int main()
{
    MyType mytype;

    mytype.set_callback(&testfunc);

    // ...
}
Last edited on Jul 19, 2011 at 4:36pm
Jul 19, 2011 at 4:44pm
Well this class and the function are both in a header file that I will be distributing to others. (think of them like the end user)

So from what I gather they will have to initialize the class with a function pointer from a function included in the header file....

It just seems so nonsensical... There must be a better way to do this somehow.
Jul 19, 2011 at 4:47pm
You would use a function pointer if you need to call one function or the other (or others) depending on the functionality that you want. If you only have a single function, meaning there is only one choice of function, why bother with a function pointer? That would be nonsense, yes. But since I am failing to understand the full picture here, I can't be sure this is what you want/are doing.
Jul 19, 2011 at 4:57pm
In situations where it doesn't make sense, then don't do it. In fact I would recommend that you avoid function pointers completely if at all possible. However if you are forced to, or for some reason it does make sense, you now know how to do it.

In your particular case you need to give us more information on what you are trying to do in order for us to suggest a better solution.
Jul 19, 2011 at 5:19pm
Well I'm using the wxWidgets library and I'm trying to bind events using the bind function. The bind function requires a pointer to a function in order to call it when an event happens. The class' constructor registers the event with the bind event.

I have a function supplied which handles the event for the user. I would like the class to just register the event and use the supplied event handling function without the user needing to manually specify it in their code.
Jul 19, 2011 at 5:54pm
any more ideas on how I might get around this or is it the only way?

Thanks for help so far btw folks!
Topic archived. No new replies allowed.