What's the use of operator()?

Discussion in 'C++' started by Olumide, Sep 23, 2004.

  1. Olumide

    Olumide Guest

    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
    Olumide, Sep 23, 2004
    #1
    1. Advertising

  2. "Olumide" <> schrieb im Newsbeitrag
    news:...
    > 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
    Gernot Frisch, Sep 23, 2004
    #2
    1. Advertising

  3. Olumide

    Olumide Guest

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

    - Olumide -
    Olumide, Sep 23, 2004
    #3
  4. Olumide

    Unforgiven Guest

    "Olumide" <> wrote in message
    news:...
    > 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
    Unforgiven, Sep 23, 2004
    #4
  5. Olumide

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Sep 23, 2004
    #5
  6. Olumide

    Rolf Magnus Guest

    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.
    Rolf Magnus, Sep 24, 2004
    #6
  7. "Olumide" <> schrieb im Newsbeitrag
    news:...
    > 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"
    Gernot Frisch, Sep 24, 2004
    #7
  8. "Rolf Magnus" <> schrieb im Newsbeitrag
    news:civol2$1pp$00$-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
    Gernot Frisch, Sep 24, 2004
    #8
  9. Olumide

    Chris Guest

    "Olumide" <> wrote in message
    news:...
    > 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
    Chris, Sep 26, 2004
    #9
  10. Olumide

    Gerhard Wesp Guest

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


    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
    Gerhard Wesp, Sep 27, 2004
    #10
  11. In message <cj8k1t$738$>, Gerhard Wesp
    <> writes
    >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.)

    >
    >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, Oct 7, 2004
    #11
  12. Olumide

    Gerhard Wesp Guest

    Richard Herring <junk@[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!
    Gerhard Wesp, Oct 13, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    560
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    410
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,032
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    4,998
    Jeff Schwab
    Mar 20, 2005
  5. Ioannis Vranos
    Replies:
    8
    Views:
    326
    James Kanze
    Nov 14, 2008
Loading...

Share This Page