How to use pointer to a function

This is a discussion on How to use pointer to a function within the C++ Programming forums, part of the General Programming Boards category; I have the following code Code: class test { int t(int ); }; int test::t(int a){ return a; } void ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    19

    How to use pointer to a function

    I have the following code
    Code:
    class test {
    	int t(int );
    };
    int test::t(int a){
    	return a;
    }
    
    void test2(int (*f)(int )){
    	printf("%d",f(2));
    }
    void main()
    {
    	test b;
    	test2(b.t);	
    }

    I want to send a pointer to the function "t" but I can't figure out how to do it

    Can somebody help ?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to use a member function inside a class like that, you really need to pass the class. A member function call has an extra hidden parameter (this) that is not present in normal function calls, so the function you are passing is not compatible with your prototype function for test2().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Class members have a much more complex syntax... I don't remember exactly how it goes - pretty much lately, I've only used templates to do this. Someone else might help you on that front.
    But your syntax is working for an ordinary non-class function pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    I think it should be:
    Code:
    #include <cstdio>
    
    using namespace std;
    
    class test {
    public:
    	int t(int);
    };
    
    int test::t(int a) {
    	return a;
    }
    
    void test2(int (test::*f)(int), test& t) {
    	printf("&#37;d", (t.*f)(2));
    }
    
    int main()
    {
    	test b;
    	test2(&test::t, b);
    }
    However, I would also ask: why do you want to do this?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    If Boost is an option, rewrite test2 as a template taking a functor and use boost::bind:

    Code:
    template <typename F>
    void test2(F f){
    	printf("%d",f(2));
    }
    void main()
    {
    	test b;
    	test2(boost::bind(&test::t, &b));
    }
    This is far preferable to passing a member function pointer. The syntax to do that is grotesque.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Use typedefs in such case. They're very useful at hiding grotesque syntax. When I use function pointers, I've almost always used typedefs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by Elysia View Post
    Use typedefs in such case. They're very useful at hiding grotesque syntax. When I use function pointers, I've almost always used typedefs.
    I don't think plain function pointers are that bad looking. It's member function pointers which I always have to go back to the book for. It's better anyway to use a binder of some kind, because it avoids having to pass "this" around.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Well yes, I think I can agree there
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    19
    Thanx for the help

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If the function pointer is even moderately complicated, typedefs are a must. For example, a function in xuni has this prototype:
    Code:
    typedef void (*init_func_t)(void *vdata, struct smode_t *smode,
        struct font_t *font, struct theme_t *theme, struct gui_t *gui,
        struct resource_t *settings);
    typedef void (*start_func_t)(void *vdata, struct theme_t *theme,
        struct gui_t *gui);
    typedef int (*event_func_t)(enum panel_type_t *mode, SDL_Event *event,
        void *vdata, struct smode_t *smode, struct font_t *font,
        struct theme_t *theme, struct gui_t *gui);
    typedef int (*set_widget_sel_func_t)(enum panel_type_t mode, int xp, int yp,
        int click, void *vdata, struct smode_t *smode, struct theme_t *theme,
        struct gui_t *gui);
    typedef int (*perform_click_func_t)(int id, enum panel_type_t *mode,
        void *vdata, struct smode_t *smode, struct gui_t *gui);
    typedef void (*paint_func_t)(void *vdata, enum panel_type_t mode,
        struct smode_t *smode, struct font_t *font, struct theme_t *theme,
        struct gui_t *gui);
    typedef void (*free_func_t)(void *vdata, struct gui_t *gui);
    
    void add_view(struct loop_data_t *data, void *vdata, int frameupdate,
        init_func_t init_func, start_func_t start_func, event_func_t event_func,
        set_widget_sel_func_t set_widget_sel_func,
        perform_click_func_t perform_click_func, paint_func_t paint_func,
        free_func_t free_func);
    That's a really badly designed function and I'm hoping to get rid of it soon, but just imagine what that function would look like without those typedefs . . . .

    And since no one's mentioned it yet that I can see: don't use void main(), use int main(). http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21