What's the use of operator()?

O

Olumide

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
 
G

Gernot Frisch

Olumide said:
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
 
U

Unforgiven

Olumide said:
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.
 
K

Kai-Uwe Bux

Olumide said:
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
 
R

Rolf Magnus

Olumide said:
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.
 
G

Gernot Frisch

Olumide said:
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"
 
G

Gernot Frisch

Rolf Magnus said:
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
 
C

Chris

Olumide said:
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
 
G

Gerhard Wesp

Olumide said:
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
 
R

Richard Herring

Gerhard Wesp said:
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.
 
G

Gerhard Wesp

Richard Herring said:
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
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top