Overloading operators

This is a discussion on Overloading operators within the C++ Programming forums, part of the General Programming Boards category; Hi guys This thread is a follow-up of http://cboard.cprogramming.com/cplus...ointers-2.html , which I have thought about for some time. If I ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Overloading operators

    Hi guys

    This thread is a follow-up of Overloading unary operators using pointers, which I have thought about for some time. If I overload an operator using a member function (this can be a unary or binary operator - it is not important what kind it is), then is the operand the this-pointer and the possible argument of the member functions prototype? I.e., is the operand for "+",

    Code:
    class test{
    public:
    void operator+(test a);
    }
    both a and the this-pointer or what?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is binary +. *this will be the left-hand operand and a will be the right-hand operand.
    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).

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    You can overload "operator int()" and do arithmetic on integers

    Code:
    #include <iostream>
    using namespace std;
    
    class Foo{
        int x;
    public:
        Foo(int x = 0):x(x){}
        operator int(){return x;}
    };
    
    int main(){
    
        Foo obj(10);
        Foo obj2 = obj + 2;
        int i = obj;
    
    
        cout << obj << endl;
        cout << obj2 + obj << endl;
        cout << i + obj << endl;
    
        return 0;
    }


    But yes, "a" is right hand side and "this" object is left hand side. Though I don't think you want to return void.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Conversion operators such as int usually is a bad idea. They do bad implicit conversions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    You can overload "operator int()" and do arithmetic on integers
    You can also throw away Foo and use integers... that's not the point of the question.

  6. #6
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Quote Originally Posted by Syscal
    But yes, "a" is right hand side and "this" object is left hand side. Though I don't think you want to return void.
    *cough*I think I know that. It was just an alternative.*cough*
    Last edited by Syscal; 09-07-2010 at 11:35 PM.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by anon View Post
    This is binary +. *this will be the left-hand operand and a will be the right-hand operand.
    Ok, but that means that the two arguments (i.e. this and a) are passed as pointers in the following example:

    Code:
    class test{
    public:
    void operator+(test* a);
    }
    Why am I allowed to overload + in the aboce case, but no in the following case where I am using a non-member function?

    Code:
    void operator+(test* a, test* b);
    I know the reason why example #2 does not work is because pointer-addition is not defined, but isn't that also what we are doing in example #1?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quote Originally Posted by Niels_M
    isn't that also what we are doing in example #1?
    It is not, since as a member function, the left hand operand would be say, an object of the test class, not a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    But anon said in #2 that the left hand operand is the this-pointer. You are of course not responsible for what others say, but if anon

    1) is correct, then I cannot see why my claim in #7 is wrong.
    2) is wrong, then what is the correct answer?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quote Originally Posted by Niels_M
    But anon said in #2 that the left hand operand is the this-pointer.
    Let's examine what anon really wrote in post #2:
    Quote Originally Posted by anon
    *this will be the left-hand operand
    anon did not write:
    Quote Originally Posted by Niels_M
    the left hand operand is the this-pointer
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    Why am I allowed to overload + in the aboce case, but no in the following case where I am using a non-member function?
    If perhaps you did this:
    Code:
    void operator + (test *a)
    {
       a->x += x;
    }
    Then the x's are added and the answer is stored in a, which is not necessarily what you want as

    c = a + b;

    is expected to work. Your operator is void, so we cannot depend on that.

    If you wanted to go non-member non-friend then
    Code:
    const test & operator + (const test &a, const test &b)
    {
       return test(a.getX() + b.getX());
    }
    is the binary + operator.

    So the answers to your question include because your original operator has no return value, and because even if you wrote an operator outside, it needs access to the member x. There are many ways to safely access x, including accessor methods, or through use of the friend keyword. You could also make x public and not write an accessor, but then you will have to make sure not to use x inappropriately.
    Last edited by whiteflags; 09-08-2010 at 12:57 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    I note that, whether it is a member or non-member, operator + should normally return the object by value, not by (const) reference, since the expected semantics is that a new object that is the "addition" of the operands is created. whiteflags' example erroneously returns a const reference to a local temporary.

    If operator+ is available, it is often reasonable to expect that operator+= would also be available. As such, one approach is to implement operator+= as a member function, and then implement operator+ as a non-member non-friend function that makes use of operator+=, e.g.,
    Code:
    test operator+(test a, const test& b)
    {
        return a += b;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    whiteflags' example erroneously returns a const reference to a local temporary.
    C++ continues to confound me to this day. Do I understand correctly that a const reference works if you were creating a new thing anyway:

    test c = a + b;

    and not if you were using assignment,

    c = a + b;

    because the temporary would be destroyed before the assignment? I was under the impression that a const reference to a local is OK if you were creating new objects, anyway. Sorry about doing something so specific OP...

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't return any reference to a local. The local's life-time ends when the scope ends (the function returns).
    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).

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    I believe that, if you create a temporary and bind it to a const reference, then the temporary's life time will be expanded until such time the const reference goes out of scope.
    However, you are not creating a reference to a temporary, but rather a local variable. Thus this rule does not hold.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 07:24 PM

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