Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > understanding function object

Reply
Thread Tools

understanding function object

 
 
Wenjie
Guest
Posts: n/a
 
      07-26-2003
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-26-2003
On 25 Jul 2003 17:33:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-26-2003
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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-26-2003
"Wenjie" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help understanding a const function returning an object reference. noobzillaking@gmail.com C++ 5 09-21-2007 11:18 AM
function Object,function statement,function operator alex Javascript 3 12-28-2006 02:57 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments