Operator Overload

Discussion in 'C++' started by Rama, Mar 23, 2005.

  1. Rama

    Rama Guest

    wht is operator overloading, how you overload an operator
     
    Rama, Mar 23, 2005
    #1
    1. Advertising

  2. Rama wrote:
    > wht is operator overloading, how you overload an operator
    >


    You define a function using the special syntax for defining an operator:

    [<return_value_type>] operator @ ( [<arguments>] )

    The keyword 'operator' and the parentheses are necessary. The return
    value type is not necessary for type conversion operators. Arguments
    may be necessary depending on what operator you overload. The '@' sign
    in the syntax designates the place where you put the operator sign. For
    example, here is the single minus overload for struct T:

    struct T {
    T operator -() const;
    };

    What book are you reading that doesn't explain operator overloading?

    V
     
    Victor Bazarov, Mar 23, 2005
    #2
    1. Advertising

  3. Rama

    osmium Guest

    "Rama" writes:

    > wht is operator overloading, how you overload an operator


    Operator overloading is a means of calling a class member function when an
    operator occurs in the source code. It allows nicer looking source code
    when there is some reasonable similarity between the operator symbols and
    some function to be performed on an object. One of the more appealing
    examples is concatenation of a string, which could be codified as '+'.
    Another nice one is that operators can be applied to complex numbers where
    there is a one to one correlation between most of the arithmetic symbols for
    real numbers and complex numbers. Beware, it is easy to be tempted to let
    operator overloading run amok and use it where there is a dubious
    relationship between the operators and the resulting action.

    A simple example is as follows:
    ---------------------------
    #include <iostream>

    using namespace std;

    class C
    {
    public:
    C(int na) {n = na;}
    bool operator>(C rhs); // rhs - right hand side
    private:
    int n;
    };
    //-----------------------
    bool C::eek:perator>(C rhs)
    {
    if(n > rhs.n)
    return true;
    else
    return false;
    }
    //=====================
    int main()
    {
    C c(1024);
    C d(2048);

    bool x, y;
    x = c>d;
    y = d>c;

    cout << x << ' ' << y << endl;
    cin.get();
    }
     
    osmium, Mar 23, 2005
    #3
  4. Rama

    Jerry Coffin Guest

    osmium wrote:

    [ ... ]

    > Operator overloading is a means of calling a class member function
    > when an operator occurs in the source code.


    Leave out "class member" in that sentence and it'll be more accurate.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Mar 23, 2005
    #4
    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. Mr Smith
    Replies:
    7
    Views:
    474
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    482
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    405
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    260
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page