C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What's the use of operator()?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Olumide
Guest





PostPosted: Thu Sep 23, 2004 9:20 pm    Post subject: What's the use of operator()? Reply with quote



I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

Anyone know where can I find tutorials on this operator? I cant seem to
find any online.

Thanks,

- Olumide

Back to top
Gernot Frisch
Guest





PostPosted: Thu Sep 23, 2004 10:08 pm    Post subject: Re: What's the use of operator()? Reply with quote




"Olumide" <50295 (AT) web (DOT) de> schrieb im Newsbeitrag
news:46713cbd1df997361d57d9aec0230f8a (AT) localhost (DOT) talkaboutprogramming.com...
Quote:
I'm studying C++ and cant seem to find much justification for
operator()?
Is it good for anything? (beyond manipulating pointers to
functions -
which are part of the C standard anyway.)

Anyone know where can I find tutorials on this operator? I cant seem
to
find any online.

Instead of creating a [][] operator I usually use the (a,b) operator.
So this way I only need one class instead of n for each brace.
Take a look at boost's spirit parser library. They use all operators
so exesively, you don't even know it's C++ after you wrote some code
with it.
-Gernot



Back to top
Olumide
Guest





PostPosted: Thu Sep 23, 2004 10:23 pm    Post subject: Re: What's the use of operator()? Reply with quote



Thanks Gernot. Got any links? I'm just a learning you see.

- Olumide -

Back to top
Unforgiven
Guest





PostPosted: Thu Sep 23, 2004 10:37 pm    Post subject: Re: What's the use of operator()? Reply with quote

"Olumide" <50295 (AT) web (DOT) de> wrote

Quote:
I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

You can use them to create functors, which the STL does extensively.

--
Unforgiven


Back to top
Kai-Uwe Bux
Guest





PostPosted: Thu Sep 23, 2004 10:47 pm    Post subject: Re: What's the use of operator()? Reply with quote

Olumide wrote:

Quote:
I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

One good thing about operator() is that objects of a class with operator()
can have different states, i.e., you can realize different functions. For
instance you could realize a class to represent quadratic polynomials like
so (unchecked code):

class QuadraticPolynomial {
private:

double a, b, c;

public:

QuadraticPolynomial ( double _a, double _b, double _c )
: a ( _a )
, b ( _b )
, c ( _c )
{}

double operator() ( double x ) const {
return( c + x*( b + x*a ) );
}

};

Now different objects of this class represent different functions. How
would you do that using function pointers?


Best

Kai-Uwe Bux


Back to top
Rolf Magnus
Guest





PostPosted: Fri Sep 24, 2004 12:10 am    Post subject: Re: What's the use of operator()? Reply with quote

Olumide wrote:

Quote:
I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

In addition to what others wrote, they can be used for optimization.
Consider the C style qsort function that takes a pointer to a comparison
function. For every compare, the function needs to be called through that
pointer, which means it cannot be inlined. The same is true when using the
std::sort template with a function pointer. OTOH, if you use std::sort with
a function object (i.e. an object that overloads operator() and thus can be
used similarly to a function), the actual call is not through a pointer and
the function can be inlined.


Back to top
Gernot Frisch
Guest





PostPosted: Fri Sep 24, 2004 7:16 am    Post subject: Re: What's the use of operator()? Reply with quote


"Olumide" <50295 (AT) web (DOT) de> schrieb im Newsbeitrag
news:5fe57c430ceced2bb3e34d0ef3e71f74 (AT) localhost (DOT) talkaboutprogramming.com...
Quote:
Thanks Gernot. Got any links? I'm just a learning you see.

- Olumide -


class CA
{
public:
operator ()(int x, int y)
{
return data[x][y];
}
int data[50][70];

};

CA a;
a(3,4) = 7;
cout << a(3,4);

Google for "C++ operator overloading"




Back to top
Gernot Frisch
Guest





PostPosted: Fri Sep 24, 2004 7:18 am    Post subject: Re: What's the use of operator()? Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> schrieb im Newsbeitrag
news:civol2$1pp$00$2 (AT) news (DOT) t-online.com...
Quote:
Olumide wrote:

I'm studying C++ and cant seem to find much justification for
operator()?
Is it good for anything? (beyond manipulating pointers to
functions -
which are part of the C standard anyway.)

In addition to what others wrote, they can be used for optimization.
Consider the C style qsort function that takes a pointer to a
comparison
function. For every compare, the function needs to be called through
that
pointer, which means it cannot be inlined. The same is true when
using the
std::sort template with a function pointer. OTOH, if you use
std::sort with
a function object (i.e. an object that overloads operator() and thus
can be
used similarly to a function), the actual call is not through a
pointer and
the function can be inlined.

Very sophisticated. Thank's a lot.
-Gernot



Back to top
Chris
Guest





PostPosted: Sun Sep 26, 2004 6:44 am    Post subject: Re: What's the use of operator()? Reply with quote


"Olumide" <50295 (AT) web (DOT) de> wrote

Quote:
I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

what would you like to do with it?

Flexibility is the name of the game: you can use it for whatever you want.
for example:

class random
{
public:
//////////// omit stuff for brevity.....
int operator() ( int x,int y ) { return data[x][y]; ); // subscript
into data
const random* operator()() const { return this;} // hey look! a
pointer!
int operator() ( int n ) { return rand() % n; } // return random
number between 0 and n
private:
int data[10][10];
};

-Chris



Back to top
Gerhard Wesp
Guest





PostPosted: Mon Sep 27, 2004 8:46 am    Post subject: Re: What's the use of operator()? Reply with quote

Olumide <50295 (AT) web (DOT) de> wrote:
Quote:
I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

Well, basically it is what we call ``syntactic sugar''. If you have a
class which represents a function, then you would want to use it as
such, i.e. write

z = f( x , y ) ;

instead of e.g.

z = f.evaluate( x , y ) ;

operator() and pointers to functions are totally unrelated concepts.

Cheers
-Gerhard

Back to top
Richard Herring
Guest





PostPosted: Thu Oct 07, 2004 10:19 am    Post subject: Re: What's the use of operator()? Reply with quote

In message <cj8k1t$738$2 (AT) esel (DOT) cosy.sbg.ac.at>, Gerhard Wesp
<gwesp (AT) cosy (DOT) sbg.ac.at> writes
Quote:
Olumide <50295 (AT) web (DOT) de> wrote:
I'm studying C++ and cant seem to find much justification for operator()?
Is it good for anything? (beyond manipulating pointers to functions -
which are part of the C standard anyway.)

Well, basically it is what we call ``syntactic sugar''. If you have a
class which represents a function, then you would want to use it as
such, i.e. write

z = f( x , y ) ;

instead of e.g.

z = f.evaluate( x , y ) ;

operator() and pointers to functions are totally unrelated concepts.

Not in C++ generic programming template-land.


There, what counts is not what you are, but how you behave.

So a templated algorithm that expects a function as its argument can be
passed _anything_ that looks syntactically like a function, whether it
is one or not.
--
Richard Herring

Back to top
Gerhard Wesp
Guest





PostPosted: Wed Oct 13, 2004 4:04 pm    Post subject: Re: What's the use of operator()? Reply with quote

Richard Herring <junk@[127.0.0.1]> wrote:
Quote:
So a templated algorithm that expects a function as its argument can be
passed _anything_ that looks syntactically like a function, whether it
is one or not.

Of course.

Cheers
-Gerhard

--
Gerhard Wesp o o Tel.: +41 (0) 43 5347636
Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/
CH-8045 Zuerich _/ See homepage for email address!

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.