what for i use operator () ?

This is a discussion on what for i use operator () ? within the C++ Programming forums, part of the General Programming Boards category; Hello,.. as an hw assignment we was told to do several operator overloading: +,-,*,[],istream,ostream this was easy .. but the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    what for i use operator () ?

    Hello,..

    as an hw assignment we was told to do several operator overloading:
    +,-,*,[],istream,ostream this was easy ..
    but the tricky one is
    Code:
    ()
    i can't understand what the brace are for ?

    afaik you will use braces in constructer or functions but what other option shold i use .


    thnx in advance
    P.s.
    i ask you couse my TA isn't answar to any mail i sent ..
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    The () is the operator. If you overload it and use it on an object it looks like you're calling a function but in fact you're calling the operator ().
    It's just like [], but with braces instead of square brackets.

    Example:
    Code:
    LRESULT EJ_win::wnd::operator() (UINT msg, LPARAM lparam, WPARAM wparam)
    {
        if(_hwnd)
        {
            return SendMessage(_hwnd, msg, lparam, wparam);
        }
    }
    Last edited by pronecracker; 06-07-2007 at 10:10 AM.
    Don't call me stupid.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Depends on what the class is for.

    The above example is not entirely clear, but here's how you might use the overloaded () operator to access data in an object representing a two dimensional array (instead of [] brackets):

    Code:
    TwoDArray my_array;
    //...
    int i = my_array(x, y);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    sorry for the long no replay ..
    the class are matrix multiplication.

    i guess it really is for two dimentianel accses. (i hope you right).
    the strange thing i can't understand that is why this won't interfeare with the constructer since the constructer is also called by ().


    thnx ppl .
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    But the constructor is called in different places than operator() would be called. If the variable is already created, you cannot call the constructor, so operator() would be called. If the variable is being declared, then the parentheses will mean that the constructor must be called. The compiler can tell the difference because there aren't any instances where both could work.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    some containers must use a function object (an object that uses an overloaded () operator) in order to do custom sorting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21