understanding function object

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

    understanding function object

    Hello,


    In the SGI STL documentation:
    struct less_mag : public binary_function <double, double, bool> {
    bool operator()(doub le x, double y) { return fabs(x) < fabs(y); }
    };

    vector<double> V;
    ...
    sort(V.begin(), V.end(), less_mag());

    I am a little confused concerning 'less_mag()': does it construct
    some less_mag object? Then what should be the constructor and could
    it be written as 'less_mag'? If it is calling operator(), I don't
    think so :-)


    Thanks for your enlightment,
    Wenjie
  • Alf P. Steinbach

    #2
    Re: understanding function object

    On 25 Jul 2003 17:33:17 -0700, gokkog@yahoo.co m (Wenjie) wrote:
    [color=blue]
    >In the SGI STL documentation:
    > struct less_mag : public binary_function <double, double, bool> {
    > bool operator()(doub le x, double y) { return fabs(x) < fabs(y); }
    > };
    >
    > vector<double> V;
    > ...
    > sort(V.begin(), V.end(), less_mag());
    >
    >I am a little confused concerning 'less_mag()': does it construct
    >some less_mag object?[/color]

    Yes.

    [color=blue]
    > Then what should be the constructor[/color]

    It's the default constructor, which is generated by the compiler.

    [color=blue]
    >and could it be written as 'less_mag'?[/color]

    No.

    [color=blue]
    >If it is calling operator(), I don't think so :-)[/color]

    The sort code calls the less_mag object's operator() which takes
    two arguments, the values to be compared.

    Comment

    • Rolf Magnus

      #3
      Re: understanding function object

      Wenjie wrote:
      [color=blue]
      > Hello,
      >
      >
      > In the SGI STL documentation:
      > struct less_mag : public binary_function <double, double, bool> {
      > bool operator()(doub le x, double y) { return fabs(x) < fabs(y); }
      > };
      >
      > vector<double> V;
      > ...
      > sort(V.begin(), V.end(), less_mag());
      >
      > I am a little confused concerning 'less_mag()': does it construct
      > some less_mag object?[/color]

      Yes.
      [color=blue]
      > Then what should be the constructor[/color]

      Since the object is empty, the constructor shouldn't do anything.
      [color=blue]
      > and could it be written as 'less_mag'?[/color]

      No. less_mag is a type, and you cannot pass types as function arguments.
      [color=blue]
      > If it is calling operator(), I don't think so :-)[/color]

      Well, you create an object of class less_mag, and sort() internally
      calls the operator() on that object.


      Comment

      • Victor Bazarov

        #4
        Re: understanding function object

        "Wenjie" <gokkog@yahoo.c om> wrote...[color=blue]
        > In the SGI STL documentation:
        > struct less_mag : public binary_function <double, double, bool> {
        > bool operator()(doub le x, double y) { return fabs(x) < fabs(y); }
        > };
        >
        > vector<double> V;
        > ...
        > sort(V.begin(), V.end(), less_mag());
        >
        > I am a little confused concerning 'less_mag()': does it construct
        > some less_mag object?[/color]

        Yes, a temporary object.
        [color=blue]
        > Then what should be the constructor and could
        > it be written as 'less_mag'?[/color]

        I am not sure I understand the second part of the question. The
        constructor's "name" is always the same as the class'. If you need
        to define a parametrised constructor, then you write

        struct less_mag : ... {

        less_mag(int parameter) : blahblah(blah) {}

        bool operator()( ...
        };
        [color=blue]
        > If it is calling operator(), I don't
        > think so :-)[/color]

        No, it's not.

        Victor


        Comment

        Working...