Passing function as argument

Discussion in 'C++' started by steven_orocos@vanderbeke.be, Nov 27, 2006.

  1. Guest

    Hi,

    I'm tryin to pass a funtion as an argument in another funtion.

    This code works
    ----------------------------------
    typedef void (*func)(int);

    void test1(int a){cout<<"1";}
    void test2(int a){cout<<"2";}
    void test3 (void (*fp) (int)) {
    (fp)(5);
    }
    void test() {
    func pf;
    pf = test1;
    test3(test1);
    }
    --------------------------------
    But when I put the cide in a class, it won't work anymore.
    -------------------------------
    typedef void (*func)(int);

    void MyClass::test1(int a){cout<<"1";}
    void MyClass::test2(int a){cout<<"2";}
    void MyClass::test3 (void (*fp) (int)) {
    (fp)(5);
    }
    void MyClass::test() {
    func pf;
    pf = test1;
    test3((func)test1);
    }
    -----------------------
    Gives me the error
    --------------
    error: argument of type 'void (MyClass::)(int)' does not match
    'void (*)(int)'
    error: invalid use of member (did you forget the '&' ?)
    --------------------

    I've tried several things, like
    test3(&test1);
    or
    typedef void (MyClass::*func)(int);


    But i can't find the solution.
    Also the net gives me several different solution, but none work..
     
    , Nov 27, 2006
    #1
    1. Advertising

  2. Ondra Holub Guest

    napsal:
    > Hi,
    >
    > I'm tryin to pass a funtion as an argument in another funtion.
    >
    > This code works
    > ----------------------------------
    > typedef void (*func)(int);
    >
    > void test1(int a){cout<<"1";}
    > void test2(int a){cout<<"2";}
    > void test3 (void (*fp) (int)) {
    > (fp)(5);
    > }
    > void test() {
    > func pf;
    > pf = test1;
    > test3(test1);
    > }
    > --------------------------------
    > But when I put the cide in a class, it won't work anymore.
    > -------------------------------
    > typedef void (*func)(int);
    >
    > void MyClass::test1(int a){cout<<"1";}
    > void MyClass::test2(int a){cout<<"2";}
    > void MyClass::test3 (void (*fp) (int)) {
    > (fp)(5);
    > }
    > void MyClass::test() {
    > func pf;
    > pf = test1;
    > test3((func)test1);
    > }
    > -----------------------
    > Gives me the error
    > --------------
    > error: argument of type 'void (MyClass::)(int)' does not match
    > 'void (*)(int)'
    > error: invalid use of member (did you forget the '&' ?)
    > --------------------
    >
    > I've tried several things, like
    > test3(&test1);
    > or
    > typedef void (MyClass::*func)(int);
    >
    >
    > But i can't find the solution.
    > Also the net gives me several different solution, but none work..


    You are trying to pass pointer to member of class as parameter. It is
    something different, than pointer to ordinary function. But you can
    still pass pointer to static member function - it works same way as
    ordinary function, because it does not need 'this' value for invocation.
     
    Ondra Holub, Nov 27, 2006
    #2
    1. Advertising

  3. On 27 Nov 2006 04:47:26 -0800, steven_orocos wrote:
    >I'm tryin to pass a funtion as an argument in another funtion.
    >But when I put the cide in a class, it won't work anymore.
    >Also the net gives me several different solution, but none work..


    Try this: http://www.newty.de/fpt/index.html

    Good luck,
    Roland Pibinger
     
    Roland Pibinger, Nov 27, 2006
    #3
  4. wrote:
    > Hi,
    >
    > I'm tryin to pass a funtion as an argument in another funtion.
    >
    > This code works
    > ----------------------------------
    > typedef void (*func)(int);
    >
    > void test1(int a){cout<<"1";}
    > void test2(int a){cout<<"2";}
    > void test3 (void (*fp) (int)) {
    > (fp)(5);
    > }
    > void test() {
    > func pf;
    > pf = test1;
    > test3(test1);
    > }
    > --------------------------------
    > But when I put the cide in a class, it won't work anymore.
    > -------------------------------
    > typedef void (*func)(int);
    >
    > void MyClass::test1(int a){cout<<"1";}
    > void MyClass::test2(int a){cout<<"2";}
    > void MyClass::test3 (void (*fp) (int)) {
    > (fp)(5);
    > }
    > void MyClass::test() {
    > func pf;
    > pf = test1;
    > test3((func)test1);
    > }
    > -----------------------
    > Gives me the error
    > --------------
    > error: argument of type 'void (MyClass::)(int)' does not match
    > 'void (*)(int)'
    > error: invalid use of member (did you forget the '&' ?)
    > --------------------
    >
    > I've tried several things, like
    > test3(&test1);
    > or
    > typedef void (MyClass::*func)(int);
    >
    >
    > But i can't find the solution.
    > Also the net gives me several different solution, but none work..


    Pointers to members differ from pointers to ordinary functions
    (and one cannot be cast to another), because member functions take
    an extra "hidden" 'this' argument.

    HTH,
    - J.
     
    Jacek Dziedzic, Nov 27, 2006
    #4
  5. Guest

    Roland Pibinger schreef:

    > Try this: http://www.newty.de/fpt/index.html
    >


    I had allready found that link
    using this code
    test3(*this.*test1);
    I get error: '((MyClass*)this)->MyClass::test1' cannot be used as a
    member pointer, since it is of type '<unknown type>'
    And this
    test3((void(*)(int))*this.*test1);
    gives me
    error: invalid cast from type 'MyClass' to type 'void (*)(int)'
     
    , Nov 27, 2006
    #5
  6. David Harmon Guest

    On 27 Nov 2006 04:47:26 -0800 in comp.lang.c++,
    wrote,
    >--------------------------------
    >But when I put the cide in a class, it won't work anymore.
    >-------------------------------
    >typedef void (*func)(int);


    typedef void (MyClass::*func)(int);

    >void MyClass::test1(int a){cout<<"1";}
    >void MyClass::test2(int a){cout<<"2";}
    >void MyClass::test3 (void (*fp) (int)) {


    void MyClass::test3 (func fp) {

    > (fp)(5);


    (this->*fp)(5);

    >}
    >void MyClass::test() {
    > func pf;
    > pf = test1;


    pf = &test1;

    > test3((func)test1);


    test3(&test1);

    This issue is covered in Marshall Cline's C++ FAQ. See the section
    "[33.5] How can I avoid syntax errors when calling a member function
    using a pointer-to-member-function? " but ignore his #define. It is
    always good to check the FAQ before posting. You can get the FAQ at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Nov 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reckoner
    Replies:
    11
    Views:
    741
    Steven D'Aprano
    Jan 19, 2009
  2. AikidoGuy
    Replies:
    11
    Views:
    595
    Seebs
    Nov 21, 2011
  3. Trans
    Replies:
    3
    Views:
    211
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    270
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    202
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page