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 11 1207
"Olumide" <50***@web.de> schrieb im Newsbeitrag
news:46******************************@localhost.ta lkaboutprogramming.com... 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
Thanks Gernot. Got any links? I'm just a learning you see.
- Olumide -
"Olumide" <50***@web.de> wrote in message
news:46******************************@localhost.ta lkaboutprogramming.com... 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
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.)
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
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.
"Olumide" <50***@web.de> schrieb im Newsbeitrag
news:5f******************************@localhost.ta lkaboutprogramming.com... 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"
"Rolf Magnus" <ra******@t-online.de> schrieb im Newsbeitrag
news:ci*************@news.t-online.com... 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
"Olumide" <50***@web.de> wrote in message
news:46******************************@localhost.ta lkaboutprogramming.com... 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
Olumide <50***@web.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.
Cheers
-Gerhard
In message <cj**********@esel.cosy.sbg.ac.at>, Gerhard Wesp
<gw***@cosy.sbg.ac.at> writes Olumide <50***@web.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
Richard Herring <ju**@[127.0.0.1]> wrote: 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven Bethard |
last post by:
I thought it might be useful to put the recent lambda threads into
perspective a bit. I was wondering what lambda gets used for in "real"
code, so I grepped my Python Lib directory. Here are some...
|
by: Roy Yao |
last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ?
According to my analysis, operator sizeof, (type) and * have the same
precedence, and they combine from right to left. Then this...
|
by: John Cho |
last post by:
// CHO, JOHN
#include<iostream>
class fracpri{
int whole;
int numer;
int denom;
|
by: John Cho |
last post by:
/* This program is a database program that will store
video tape names, minutes, year released, and price
I want it to be professional so that *YOU* will want
to buy it and use it for a video...
|
by: dru |
last post by:
Every time I think I know or understand C++ with some
confidence, I see something that changes that.
I was looking at the boost shared_ptr docs and code, and
I ran across the operator...
|
by: Steven T. Hatton |
last post by:
I read Stroustrup's article of the day:
http://www.research.att.com/~bs/C++.html
Programming with Exceptions. InformIt.com. April 2001.
http://www.research.att.com/~bs/eh_brief.pdf
Some of...
|
by: Anoob |
last post by:
Can we consider () unary operator when calling a function, in exps eq.
f(), ( 1 + 2). But when we call function f( 10 ) it is a binary
operator. Even if we pass f( 10, 20) as we are using ,...
|
by: stella_pigeon |
last post by:
Can someone offer me a hint how to correctly program the problem
outlined below?
I have a template class defines as follows
class A {
int a1;
int a2;
operator == ...
}
|
by: Ajay |
last post by:
Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.
...
|
by: Peng Yu |
last post by:
Hi,
In the following code, the 'copy' member function works. But the '='
operator does not work. Can somebody let me know why a member function
is different from an operator.
Thanks,
Peng
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |