• Advertisement

Archived

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

Storing function pointers in a class?

This topic is 5128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Ahh, it''s the having to typedef it that was getting me.

Thanks, works like a charm.

- Newb Programmer: Geek++

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

  • Advertisement