understanding function object

W

Wenjie

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
 
A

Alf P. Steinbach

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.
 
R

Rolf Magnus

Wenjie said:
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.
 
V

Victor Bazarov

Wenjie said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top