Thread: Overloading operators

  1. #16
    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.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't make sense of your types.
    There are two types T* and T&. T* != T&.
    this = T*, *this = T&.
    Knowing that, if you're still confused, then rephrase your question with proper type usage.

    To be more precise, if we define an operator to take a left-hand parameter (lhs) and a right-hand parameter (rhs) [for example, operator + (lhs, rhs)], then,
    T&, T* is allowed
    T*, T* is not allowed
    Last edited by Elysia; 09-08-2010 at 02:24 PM.
    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.

  3. #18
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Ok, I will provide an almost-full example to avoid confusion

    Code:
    #include <iostream>
    
    class test{
    public:
    	int a;
            void operator-(test* a)                           // ALLOWED
    	friend void operator+(test* a. test* b); // NOT ALLOWED
    };
    
    
    int main()
    {
            test c, d;
            .......
            c-d; //c is transferred using the this-pointer, and d is the right-hand operand, i.e. test* a        
    	return 0;
    }
    When using c-d, both c and d are passed as pointers. So my question is still: Why are we allowed to do that in the member-function-case, but not in the non-member function case?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This will not compile. Ignoring the syntax error, which you probably didn't mean, c and d are passed as test&, which is not test*.
    Again, note the difference between T* and T&.

    In case of member operators, the lhs is *this, not this. this is T*, which means that *this is test which could be passed as test&.
    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. #20
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Sorry, sorry, sorry. I meant to write (&c)-(&d).

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point is the same (note: inside class):

    void operator - (test*);

    ...is the same as (note: outside class)...

    void operator - (test&, test*);

    ...and...

    friend void operator + (test*, test*);

    ...is the same as...

    void operator + (test*, test*);

    ...and therefore not allowed.

    Try this:
    Code:
    class test
    {
    public:
    	int a;
    	void operator + (test*);
    	void operator - (test*);
    	//friend void operator + (test*, test*);
    	//friend void operator - (test*, test*);
    };
    
    void operator + (test&, test*);
    void operator - (test&, test*);
    //void operator + (test*, test*);
    //void operator - (test*, test*);
    
    int main()
    {
        test c, d;
        (&c - &d); //c is transferred using the this-pointer, and d is the right-hand operand, i.e. test* a
    	return 0;
    }
    Remove any comment and it fails to compile.
    First two class operators and globals operators are equivalent. Second class operators and globals operators are also equivalent.
    Last edited by Elysia; 09-10-2010 at 12:14 PM.
    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.

  7. #22
    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"

  8. #23
    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.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You access the value of this by typing this and access the object it points to by *this.
    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.

  10. #25
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elysia View Post
    You access the value of this by typing this and access the object it points to by *this.
    Yes, but the value of this is the address of its object, which is why I cannot understand the claim made by iMalc, i.e. that it is being passed as a reference.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    iMalc claims that the object itself is being passed by reference, and no its address, because to access the object, we always have to dereference a pointer to it, or access it directly. Hence no address.
    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.

  12. #27
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elysia View Post
    iMalc claims that the object itself is being passed by reference, and no its address, because to access the object, we always have to dereference a pointer to it, or access it directly. Hence no address.
    I don't see what the difference is between this explanation and my claim that this is a pointer, not a reference. I really don't.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    this is a pointer, *this is a reference. But the point is that when an overloaded operator is called, *this is passed, not this.
    And the reason is as iMalc explained. In order to access the object to perform the operation on, the compiler has to dereference this first, so it then passes *this.
    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.

  14. #29
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Is it always the case that if

    Code:
    int *ptr;
    then ptr is the pointer and *ptr the reference? If yes, I did not know that.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Niels_M
    then ptr is the pointer and *ptr the reference?
    I would rather say that ptr is the pointer and *ptr is what the pointer points to.
    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