Thread: Overloading operators

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    266
    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
    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?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Niels_M View Post
    Why am I allowed to overload + in the aboce case, but no in the following case where I am using a non-member function?

    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?
    Because the evolution of the C++ language includes in its history, a few mistakes. One of them is that 'this' should have been a reference rather than a pointer.

    As you know, calling any member function has an implicit 'this' pointer:
    Code:
    class foo {
    public:
        int bar;
        void foobar() { cout << "woot!" << this->bar << endl; }
    };
    
    foo f;
    f.foobar();
    Here, foobar is called on an object, not a pointer, and yet C++ gives us use of 'this', which is a pointer.

    We could just as easily have called it like this:
    Code:
    foo *pf;
    pf = new foo();
    pf->foobar();
    delete pf;
    However, here we're dereferencing pf using the -> operator to call the function, so we're essentially going from a pointer to what it points to, in order to make the call, losing all knowledge that the call even came from a pointer.

    So basically, whether it is to an operator overload as a member function, or a plain old member function, the call is conceptually made on the object itself, not on a pointer.
    Now, given that we could not have the implicit parameter being passed by value, clearly it must be passed by reference.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by iMalc View Post
    Now, given that we could not have the implicit parameter being passed by value, clearly it must be passed by reference.
    I am not sure I understand that 100%, because I still access the value of this by *this.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  9. #9
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    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?

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    Let's examine what anon really wrote in post #2:

    anon did not write:
    My bad, I was being (very) inaccurate there. But I still believe my question possesses some validity (when stated correctly).

    In my example #1 (in post #7), *this (a pointer to the left variable) will be the left-hand operand and *a (a pointer to the right variable) will be the right hand operator.

    In my example #2, *a (a pointer to the left variable) will be the left-hand operand and *b (a pointer to the right variable) will be the right hand operator.


    So, now that I am being more precise, what is the difference between #1 and #2? I presume the answer is what you gave in #8: ".. since as a member function, the left hand operand would be say, an object of the test class, not a pointer.", but I am not quite sure I understand that fully. Can you perhaps elaborate it a little?


    whiteflags: I'm not quite sure I understand, but lets see what the response is to the above first.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 01: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, 08:24 PM