Function pointer

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

    #1

    Function pointer

    Hello everyone,


    In the following statements,

    Code:
    template <class R, class Tclass mem_fun_t : public unary_function
    <T*, R>
    
    {
    
    R (T::*pmf)()
    ....
    }
    1. I think pmf is a type of function pointer, the return type of the
    function is R and the function is a member function of type (class) T.
    Is my understanding correct?

    2. If yes, what is the parameter list of the function? Empty parameter
    list?

    3. I doubt whether it is useful to define a function pointer with
    empty parameter list -- too restricted.


    thanks in advance,
    George
  • Rolf Magnus

    #2
    Re: Function pointer

    George2 wrote:
    Hello everyone,
    >
    >
    In the following statements,
    >
    Code:
    template <class R, class Tclass mem_fun_t : public unary_function
    <T*, R>
    >
    {
    >
    R (T::*pmf)()
    ...
    }
    >
    1. I think pmf is a type of function pointer,
    With a semicolon added to the end of that line, pmf would be the name of a
    member variable of mem_fun_t.
    the return type of the function is R and the function is a member function
    of type (class) T.
    Yes.
    2. If yes, what is the parameter list of the function? Empty parameter
    list?
    Yes.
    3. I doubt whether it is useful to define a function pointer with
    empty parameter list -- too restricted.
    Well, it is restricted to functions that have exactly 0 parameters. If you
    defined a pointer to function with 10 parameters, it would be restricted to
    functions that take exactly 10 parameters.

    Comment

    • Michael DOUBEZ

      #3
      Re: Function pointer

      George2 a écrit :
      Hello everyone,
      >
      >
      In the following statements,
      >
      Code:
      template <class R, class Tclass mem_fun_t : public unary_function
      <T*, R>
      >
      {
      >
      R (T::*pmf)()
      ...
      }
      >
      1. I think pmf is a type of function pointer, the return type of the
      function is R and the function is a member function of type (class) T.
      Is my understanding correct?
      yes.
      In brief it iss a pointer on a member fucntion of class T.
      >
      2. If yes, what is the parameter list of the function? Empty parameter
      list?
      Yes. No parameters.
      >
      3. I doubt whether it is useful to define a function pointer with
      empty parameter list -- too restricted.
      It is useful if you want to call a method of a class that takes no
      parameters.

      struct foo
      {
      static int gcounter=0;

      foo(){id_=gcoun ter++;}

      void bar(){std::cout <<"My id is "<<id_<<std::en dl;}

      int id_;
      };


      std::vector<foo v(10);

      std::for_each(v .begin(),v.end( ),std::mem_fun_ ref(&foo::bar)) ;


      Of course, you could put parameters in a class and make it a wrapper
      class of a more elaborate functor.

      Another example from SGI:


      struct B {
      virtual void print() = 0;
      };

      struct D1 : public B {
      void print() { cout << "I'm a D1" << endl; }
      };

      struct D2 : public B {
      void print() { cout << "I'm a D2" << endl; }
      };

      int main()
      {
      vector<B*V;

      V.push_back(new D1);
      V.push_back(new D2);
      V.push_back(new D2);
      V.push_back(new D1);

      for_each(V.begi n(), V.end(), mem_fun(&B::pri nt));
      }

      Michael

      Comment

      • Pete Becker

        #4
        Re: Function pointer

        On 2007-12-10 06:48:11 -0500, George2 <george4academi c@yahoo.comsaid :
        Hello everyone,
        >
        >
        In the following statements,
        >
        Code:
        template <class R, class Tclass mem_fun_t : public unary_function
        <T*, R>
        >
        {
        >
        R (T::*pmf)()
        ...
        }
        >
        1. I think pmf is a type of function pointer,
        No. It's a pointer to member function, which is not at all like a
        pointer to (non-member) function.
        the return type of the
        function is R and the function is a member function of type (class) T.
        Is my understanding correct?
        Right.
        >
        2. If yes, what is the parameter list of the function? Empty parameter
        list?
        Yes.
        >
        3. I doubt whether it is useful to define a function pointer with
        empty parameter list -- too restricted.
        >
        Read about what mem_fun_t is used for. The definition of pmf is exactly
        what's needed.

        --
        Pete
        Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
        Standard C++ Library Extensions: a Tutorial and Reference
        (www.petebecker.com/tr1book)

        Comment

        Working...