How to pass a function name as an argument?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yangang Bao

    How to pass a function name as an argument?

    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(doubl e(*func)(double ), double a, double b);
    double foo1(double t);

    public:
    double drive();

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

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

    double Funct::foo1(dou ble 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;
    }
    *************** *************** ********


  • Josephine Schafer

    #2
    Re: How to pass a function name as an argument?


    "Yangang Bao" <ybao@ualberta. ca> wrote in message
    news:bmdice$i1l $1@tabloid.srv. ualberta.ca...[color=blue]
    > 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(doubl e(*func)(double ), double a, double b);
    > double foo1(double t);
    >
    > public:
    > double drive();
    >
    > };
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > file://Funct.cpp
    > ////////////////////////////////////////////////////////////////////////////
    > /
    > #include "Funct.h"
    >
    > double Funct::Integrat e(double (*func)(double) , double a, double b)
    > {
    > double c = a+b;
    > return func(c);
    > }
    >
    > double Funct::foo1(dou ble 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;
    > }[/color]

    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::*)(doub le).
    Pointers to non-static member functions are not type compatible with regular
    pointers to functions, hence the error.

    HTH,
    J.Schafer


    Comment

    • John Carson

      #3
      Re: How to pass a function name as an argument?

      "Yangang Bao" <ybao@ualberta. ca> wrote in message
      news:bmdice$i1l $1@tabloid.srv. ualberta.ca[color=blue]
      > 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,[/color]

      The C language doesn't have member functions, so the issue never arises.
      [color=blue]
      > i.e., in the program, if I call "foo2", it works fine. Why "foo1"
      > doesn't work?[/color]

      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]
      [color=blue]
      >
      > class Funct
      > {
      > double Integrate(doubl e(*func)(double ), double a, double b);[/color]

      // Try the following overload:

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

      // which you define as

      double Funct::Integrat e(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)

      Comment

      • Yangang Bao

        #4
        Re: How to pass a function name as an argument?

        It seems it works. You are really helpful. Thanks a lot.


        Comment

        • Yangang Bao

          #5
          Re: How to pass a function name as an argument?

          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(doub le (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(dou ble t)
          {
          return t*t;
          }

          double Funct::Quadratu re(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" <ybao@ualberta. ca> wrote in message
          news:bmfr0v$le9 $1@tabloid.srv. ualberta.ca...[color=blue]
          > It seems it works. You are really helpful. Thanks a lot.
          >
          >[/color]


          Comment

          • tom_usenet

            #6
            Re: How to pass a function name as an argument?

            On Tue, 14 Oct 2003 12:10:35 -0600, "Yangang Bao" <ybao@ualberta. ca>
            wrote:
            [color=blue]
            >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++.[/color]
            [color=blue]
            >double Funct::Quadratu re(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);[/color]

            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.
            [color=blue]
            > return s;
            >}
            >
            >double Funct::QuadStep (double (Funct::*func)( double), double c, double d)
            >{
            > return (this->*func)(c*d);[/color]

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

            Tom

            Comment

            Working...