Storing function pointers in a class?

Started by
2 comments, last by GeekPlusPlus 20 years ago
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++
- Newb Programmer: Geek++
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;}; 
Ahh, it''s the having to typedef it that was getting me.

Thanks, works like a charm.

- Newb Programmer: Geek++
- Newb Programmer: Geek++
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);}; 

This topic is closed to new replies.

Advertisement