A question about operator overloading?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dolphin

    #1

    A question about operator overloading?

    Hi All
    I just read the C++FAQ . I have something confused. The question
    is that :

    If you provide constructive operators, they should allow promotion of
    the left-hand operand (at least in the case where the class has a
    single-parameter ctor that is not marked with the explicit keyword).
    For example, if your class Fraction supports promotion from int to
    Fraction (via the non-explicit ctor Fraction::Fract ion(int)), and if
    you allow x - y for two Fraction objects, you should also allow 42 -
    y. In practice that simply means that your operator-() should not be a
    member function of Fraction. Typically you will make it a friend, if
    for no other reason than to force it into the public: part of the
    class, but even if it is not a friend, it should not be a member.


    Why the operator-() should not be a member function? Can someone give
    a example?
  • Abhishek Padmanabh

    #2
    Re: A question about operator overloading?

    On Dec 6, 8:00 pm, dolphin <jdxyw2...@gmai l.comwrote:
    Hi All
    I just read the C++FAQ . I have something confused. The question
    is that :
    >
    If you provide constructive operators, they should allow promotion of
    the left-hand operand (at least in the case where the class has a
    single-parameter ctor that is not marked with the explicit keyword).
    For example, if your class Fraction supports promotion from int to
    Fraction (via the non-explicit ctor Fraction::Fract ion(int)), and if
    you allow x - y for two Fraction objects, you should also allow 42 -
    y. In practice that simply means that your operator-() should not be a
    member function of Fraction. Typically you will make it a friend, if
    for no other reason than to force it into the public: part of the
    class, but even if it is not a friend, it should not be a member.
    >
    Why the operator-() should not be a member function? Can someone give
    a example?
    Aah, another thread with the same issue. See this one -


    Short answer: It can not be a member because the member would never
    get invoked!

    Comment

    • dolphin

      #3
      Re: A question about operator overloading?

      On 12ÔÂ6ÈÕ, ÏÂÎç11ʱ32·Ö, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
      wrote:
      On Dec 6, 8:00 pm, dolphin <jdxyw2...@gmai l.comwrote:
      >
      >
      >
      >
      >
      Hi All
      I just read the C++FAQ . I have something confused. The question
      is that :
      >
      If you provide constructive operators, they should allow promotion of
      the left-hand operand (at least in the case where the class has a
      single-parameter ctor that is not marked with the explicit keyword).
      For example, if your class Fraction supports promotion from int to
      Fraction (via the non-explicit ctor Fraction::Fract ion(int)), and if
      you allow x - y for two Fraction objects, you should also allow 42 -
      y. In practice that simply means that your operator-() should not be a
      member function of Fraction. Typically you will make it a friend, if
      for no other reason than to force it into the public: part of the
      class, but even if it is not a friend, it should not be a member.
      >
      Why the operator-() should not be a member function? Can someone give
      a example?
      >
      Aah, another thread with the same issue. See this one -http://groups.google.c om/group/comp.lang.c++/browse_thread/thread/138...
      >
      Short answer: It can not be a member because the member would never
      get invoked!- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
      >
      - ÏÔʾÒýÓõÄÎÄ×Ö -
      Do you mean that operator+(T , T) and operator*(T,T) also should not
      be member function?

      Comment

      • David Harmon

        #4
        Re: A question about operator overloading?

        On Thu, 6 Dec 2007 07:00:20 -0800 (PST) in comp.lang.c++, dolphin
        <jdxyw2004@gmai l.comwrote,
        >Why the operator-() should not be a member function? Can someone give
        >a example?
        #include <complex>
        int main()
        {
        std::complex<do ublev(0.5);
        v - 1.0; // works whether or not - is a member
        1.0 - v; // Works only if - is a non-member.
        }

        Comment

        Working...