Archived

This topic is now archived and is closed to further replies.

GeekPlusPlus

Storing function pointers in a class?

Recommended Posts

Hello, the topic pretty much says it all. I''m curious as to how i''d define a member variable in a class so that I could pass the class a function pointer and it''ll hold that pointer. Hope someone can help. - Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
Pretty much the same way you put any variable in a class.


class ClassWithFunctionPointer {
public:
typedef void (*void_ptr)(void);

void set_function_pointer(void_ptr ptr) {
my_pointer = ptr;
}

private:
void_ptr my_pointer;
};

Share this post


Link to post
Share on other sites
It''s entirely possible to do it without the typedef - it just looks ugly as sin.

class ClassWithFunctionPointer {
public:
void set_function_pointer(void (*ptr)(void)) {
my_pointer = ptr;
}

private:
void (*my_pointer)(void);
};

Share this post


Link to post
Share on other sites