How to pass a function name as an argument?

Discussion in 'C++' started by Yangang Bao, Oct 13, 2003.

  1. Yangang Bao

    Yangang Bao Guest

    I have a simple test program. It doesn't work. I want to know what is the
    reason and how to
    fix it. Maybe I should use template function to do it. But I don't know how.

    Here is the simple program. Strange enough that the problem never happens in
    C languge,
    i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't
    work?

    Thanks for your answers.
    ////////////////////////////////////////////////////////////////////////////
    /
    file://main.cpp
    ////////////////////////////////////////////////////////////////////////////
    /
    #include <stdio.h>
    #include <stdlib.h>
    #include "Funct.h"

    void main()
    {
    double s;
    Funct funct;
    s = funct.drive();
    printf("Result= %lf", s);
    }

    ////////////////////////////////////////////////////////////////////////////
    /
    file://Funct.h
    ////////////////////////////////////////////////////////////////////////////
    /
    double foo2(double t);

    class Funct
    {
    double Integrate(double(*func)(double), double a, double b);
    double foo1(double t);

    public:
    double drive();

    };
    ////////////////////////////////////////////////////////////////////////////
    /
    file://Funct.cpp
    ////////////////////////////////////////////////////////////////////////////
    /
    #include "Funct.h"

    double Funct::Integrate(double (*func)(double), double a, double b)
    {
    double c = a+b;
    return func(c);
    }

    double Funct::foo1(double t)
    {
    return t*t;
    }

    double Funct::drive()
    {
    double t = Integrate(foo1, 2, 3);
    // double t = Integrate(foo2, 2, 3);
    return t;
    }

    double foo2(double t)
    {
    return t*t;
    }
    **************************************
     
    Yangang Bao, Oct 13, 2003
    #1
    1. Advertising

  2. Yangang Bao

    John Carson Guest

    "Yangang Bao" <> wrote in message
    news:bmdice$i1l$
    > I have a simple test program. It doesn't work. I want to know what is
    > the reason and how to
    > fix it. Maybe I should use template function to do it. But I don't
    > know how.
    >
    > Here is the simple program. Strange enough that the problem never
    > happens in C languge,


    The C language doesn't have member functions, so the issue never arises.

    > i.e., in the program, if I call "foo2", it works fine. Why "foo1"
    > doesn't work?


    Because it is a member function, not a "free" function. The syntax for
    pointers to member functions is very different to the syntax for pointers to
    ordinary functions.

    [snip]

    >
    > class Funct
    > {
    > double Integrate(double(*func)(double), double a, double b);


    // Try the following overload:

    double Integrate(double(Funct::*func)(double), double a, double b);

    // which you define as

    double Funct::Integrate(double (Funct::*func)(double), double a, double b)
    {
    double c = a+b;
    return (this->*func)(c);
    }


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Oct 13, 2003
    #2
    1. Advertising

  3. "Yangang Bao" <> wrote in message
    news:bmdice$i1l$...
    > I have a simple test program. It doesn't work. I want to know what is the
    > reason and how to
    > fix it. Maybe I should use template function to do it. But I don't know how.
    >
    > Here is the simple program. Strange enough that the problem never happens in
    > C languge,
    > i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't
    > work?
    >
    > Thanks for your answers.
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > file://main.cpp
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include "Funct.h"
    >
    > void main()
    > {
    > double s;
    > Funct funct;
    > s = funct.drive();
    > printf("Result= %lf", s);
    > }
    >
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > file://Funct.h
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > double foo2(double t);
    >
    > class Funct
    > {
    > double Integrate(double(*func)(double), double a, double b);
    > double foo1(double t);
    >
    > public:
    > double drive();
    >
    > };
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > file://Funct.cpp
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > #include "Funct.h"
    >
    > double Funct::Integrate(double (*func)(double), double a, double b)
    > {
    > double c = a+b;
    > return func(c);
    > }
    >
    > double Funct::foo1(double t)
    > {
    > return t*t;
    > }
    >
    > double Funct::drive()
    > {
    > double t = Integrate(foo1, 2, 3);
    > // double t = Integrate(foo2, 2, 3);
    > return t;
    > }
    >
    > double foo2(double t)
    > {
    > return t*t;
    > }


    See the first parameter of Integrate.
    It is a pointer to a function that takes a double as parameter and returns a
    double.
    What is the type of foo1?
    It is double (Funct::*)(double).
    Pointers to non-static member functions are not type compatible with regular
    pointers to functions, hence the error.

    HTH,
    J.Schafer
     
    Josephine Schafer, Oct 13, 2003
    #3
  4. Yangang Bao

    Yangang Bao Guest

    It seems it works. You are really helpful. Thanks a lot.
     
    Yangang Bao, Oct 14, 2003
    #4
  5. Yangang Bao

    Yangang Bao Guest

    I tried to adopt this method in my program. And unfortunately a tragedy
    happens. The problem is that I have to pass the function name as argument
    twice.
    And the VC6.0 compiler still shows the wrong message: INTERNAL COMPILER
    ERROR. I just wonder how other guys implemented the quadrature
    functions in C++.

    Here is the simple test program:
    //////////////////////////////////////////////////////////////////////////
    // main.cpp
    //////////////////////////////////////////////////////////////////////////
    #include <stdio.h>
    #include <stdlib.h>

    #include "Funct.h"

    void main()
    {
    Funct funct;
    funct.drive();
    }

    //////////////////////////////////////////////////////////////////////////
    // Funct.h
    //////////////////////////////////////////////////////////////////////////
    #ifndef FUNCT_INCLUDED_
    #define FUNCT_INCLUDED_

    class Funct
    {
    public:
    double Quadrature(double (Funct::*func)(double), double a, double b);
    double QuadStep(double (Funct::*func)(double), double c, double d);

    double foo1(double t);

    double drive();
    };

    #endif

    //////////////////////////////////////////////////////////////////////////
    // Funct.cpp
    //////////////////////////////////////////////////////////////////////////
    #include "Funct.h"

    double Funct::drive()
    {
    double a = 1;
    double b = 2;
    double s;

    s = Quadrature(foo1, a, b);
    return s;

    }

    double Funct::foo1(double t)
    {
    return t*t;
    }

    double Funct::Quadrature(double (Funct::*func)(double), double a, double b)
    {
    double c= (a+b)/2.0;
    double s1, s2;

    s1 = QuadStep((this->*func), a, c);
    s2 = QuadStep((this->*func), c, d);
    return s;
    }

    double Funct::QuadStep(double (Funct::*func)(double), double c, double d)
    {
    return (this->*func)(c*d);
    }


    "Yangang Bao" <> wrote in message
    news:bmfr0v$le9$...
    > It seems it works. You are really helpful. Thanks a lot.
    >
    >
     
    Yangang Bao, Oct 14, 2003
    #5
  6. Yangang Bao

    tom_usenet Guest

    On Tue, 14 Oct 2003 12:10:35 -0600, "Yangang Bao" <>
    wrote:

    >I tried to adopt this method in my program. And unfortunately a tragedy
    >happens. The problem is that I have to pass the function name as argument
    >twice.
    >And the VC6.0 compiler still shows the wrong message: INTERNAL COMPILER
    >ERROR. I just wonder how other guys implemented the quadrature
    >functions in C++.


    >double Funct::Quadrature(double (Funct::*func)(double), double a, double b)
    >{
    > double c= (a+b)/2.0;
    > double s1, s2;
    >
    > s1 = QuadStep((this->*func), a, c);
    > s2 = QuadStep((this->*func), c, d);


    The above two should be:

    s1 = QuadStep(func, a, c);
    s2 = QuadStep(func, c, d);

    You only dereference the pointer to member when calling it.

    > return s;
    >}
    >
    >double Funct::QuadStep(double (Funct::*func)(double), double c, double d)
    >{
    > return (this->*func)(c*d);


    That's fine, since you're calling it.

    Tom
     
    tom_usenet, Oct 14, 2003
    #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. Ben Kial
    Replies:
    1
    Views:
    676
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,929
    Eric Sosman
    Jan 7, 2005
  3. Reckoner
    Replies:
    11
    Views:
    717
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    571
    Seebs
    Nov 21, 2011
  5. Replies:
    8
    Views:
    125
Loading...

Share This Page