understanding function object

Discussion in 'C++' started by Wenjie, Jul 26, 2003.

  1. Wenjie

    Wenjie Guest

    Hello,


    In the SGI STL documentation:
    struct less_mag : public binary_function<double, double, bool> {
    bool operator()(double 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
    Wenjie, Jul 26, 2003
    #1
    1. Advertising

  2. On 25 Jul 2003 17:33:17 -0700, (Wenjie) wrote:

    >In the SGI STL documentation:
    > struct less_mag : public binary_function<double, double, bool> {
    > bool operator()(double 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?


    Yes.


    > Then what should be the constructor


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


    >and could it be written as 'less_mag'?


    No.


    >If it is calling operator(), I don't think so :)


    The sort code calls the less_mag object's operator() which takes
    two arguments, the values to be compared.
    Alf P. Steinbach, Jul 26, 2003
    #2
    1. Advertising

  3. Wenjie

    Rolf Magnus Guest

    Wenjie wrote:

    > Hello,
    >
    >
    > In the SGI STL documentation:
    > struct less_mag : public binary_function<double, double, bool> {
    > bool operator()(double 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?


    Yes.

    > Then what should be the constructor


    Since the object is empty, the constructor shouldn't do anything.

    > and could it be written as 'less_mag'?


    No. less_mag is a type, and you cannot pass types as function arguments.

    > If it is calling operator(), I don't think so :)


    Well, you create an object of class less_mag, and sort() internally
    calls the operator() on that object.
    Rolf Magnus, Jul 26, 2003
    #3
  4. "Wenjie" <> wrote...
    > In the SGI STL documentation:
    > struct less_mag : public binary_function<double, double, bool> {
    > bool operator()(double 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?


    Yes, a temporary object.

    > Then what should be the constructor and could
    > it be written as 'less_mag'?


    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()( ...
    };

    > If it is calling operator(), I don't
    > think so :)


    No, it's not.

    Victor
    Victor Bazarov, Jul 26, 2003
    #4
    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. James Vanns
    Replies:
    7
    Views:
    7,003
    Evan Carew
    Jan 21, 2004
  2. Morten Snedker

    Understanding New on the List-object

    Morten Snedker, Jan 22, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    282
    Mark Rae
    Jan 22, 2007
  3. sks
    Replies:
    4
    Views:
    395
    Alf P. Steinbach
    Jul 6, 2006
  4. Replies:
    5
    Views:
    379
    Earl Purple
    Sep 21, 2007
  5. alex
    Replies:
    3
    Views:
    514
    Richard Cornford
    Dec 28, 2006
Loading...

Share This Page