Results 1 to 5 of 5

Thread: pointer to member function

  1. #1
    Join Date
    May 2012
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default pointer to member function

    I want to use in my class a pointer to function.
    I could not find a way.
    This is a simple example that explains the difficulty:
    Qt Code:
    1. class C{
    2. public:
    3. int First (int i, int j){return i;}
    4. int Second(int i, int j){return j;}
    5. int Fun(int);
    6. };
    7. int C::Fun(int i){
    8. int (C::*pFun)(int, int);
    9. if(i==1)
    10. pFun=&C::First;
    11. else
    12. pFun=&C::Second;
    13. return pFun(1,2); //ERROR
    14. }
    To copy to clipboard, switch view to plain text mode 
    this causes an error message to be displayed.
    I cannot find a way to solve this. On the other hand the non-member version works correctly, as follows:

    Qt Code:
    1. int First(int i, int j);
    2. int Second(int i, int j);
    3. int First(int i, int j){ return i;}
    4. int Second(int i, int j){ return j;}
    5.  
    6. int main (int argc, const char * argv[])
    7. {
    8. int i=1;
    9. float x;
    10. int (*fun)(int, int);
    11. if(i==1)
    12. fun=First;
    13. else
    14. fun=Second;
    15. x=fun(1,2); //OK
    16. }
    To copy to clipboard, switch view to plain text mode 
    Does anyone have any suggestion to give?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: pointer to member function

    I assume your requirement is more complicated than your example because you don't need function pointers to solve it as written.

    You can have a simple function pointer to a free function or a static member function. Simple function pointers cannot reference a member function because they lack the "this" pointer to the instance of the object.

    There are several approaches to this. One is in the TR1 extensions to the C++ library:
    Qt Code:
    1. #include <tr1/functional>
    2.  
    3. int C::Fun(int i){
    4. std::tr1::function<int(int,int)> pFun;
    5. if(i==1)
    6. pFun = std::tr1::bind(&C::First,
    7. this,
    8. std::tr1::placeholders::_1,
    9. std::tr1::placeholders::_2);
    10. else
    11. pFun = std::tr1::bind(&C::Second,
    12. this,
    13. std::tr1::placeholders::_1,
    14. std::tr1::placeholders::_2);
    15. return pFun(1,2);
    16. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: pointer to member function

    to use pointers to member functions you also need to have an instance!
    Qt Code:
    1. class C{
    2. public:
    3. int First (int i, int j){return i;}
    4. int Second(int i, int j){return j;}
    5. int Fun(int);
    6. };
    7. int C::Fun(int i){
    8. int (C::*pFun)(int, int);
    9. if(i==1)
    10. pFun=&C::First;
    11. else
    12. pFun=&C::Second;
    13. return this->*pFun(1,2); //<<<<<<<<<<<<<
    14. }
    To copy to clipboard, switch view to plain text mode 

    untested...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    May 2012
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: pointer to member function

    ChrisW67, thank you very much for your complete explanation of the issue.
    I can accept to convert the functions to static and everything works.

    BTW, just in case you are curious, my mechanism of function pointer is inside a routine that calculates "round" numbers for the extrema of plots.
    For instance a plot that ranges between 2.345 and 32.84 could not have on the axes those nasty float numbers. Better to choose, in this case, to plot between 0 and 40. To understand what extrema to choose in all possible situations is a demanding task and I decided to take advantage to a choice of functions to select from at run time.
    To reduce the number of if's in my code I prefer to point to the right function and use later the pointer instead of the native function.

    Amleto, your suggestion did not work. I think the reason its the one explained by ChrisW67.
    Thanks again to both for helping me.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: pointer to member function

    no its just a syntax mistake.

    this works:
    return (this->*pFun)(1,2);
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 28th March 2012, 21:47
  2. Replies: 2
    Last Post: 23rd February 2012, 12:23
  3. Replies: 8
    Last Post: 15th October 2011, 18:23
  4. Pointer to non static member functions within the class
    By Lykurg in forum General Programming
    Replies: 3
    Last Post: 24th April 2011, 07:37
  5. Replies: 22
    Last Post: 8th October 2008, 13:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.