Overloading unary operators

This is a discussion on Overloading unary operators within the C++ Programming forums, part of the General Programming Boards category; Hi Please take a look at the following example. Code: #include <iostream> class test{ public: int integer; void operator-(); }; ...

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

    Overloading unary operators

    Hi

    Please take a look at the following example.

    Code:
    #include <iostream>
    
    class test{
    public:
    	int integer;
    	void operator-();
    };
    
    void test::operator-() {
    	(*this).integer++;
    
    }
    
    int main() {
    	test temp;
    	temp.integer = 1;
            +temp;
    	cout << temp.integer << endl;
    		
    	getch();
    	return 0;
    }
    When talking about binary operators (overloaded by member functions), the left argument is passed by reference to the *this-pointer. But in the case with unary operators, it is the right argument that is passed to the *this-pointer?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Niels_M
    When talking about binary operators (overloaded by member functions), the left argument is passed by reference to the *this-pointer.
    Sounds like the opposite to me, i.e., *this is passed as the "left argument".

    Quote Originally Posted by Niels_M
    But in the case with unary operators, it is the right argument that is passed to the *this-pointer?
    Yes. I mean, if the operator does not have a left operand, then it just does not have a left operand, so it is a no brainer to deduce what the argument must be.
    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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    Sounds like the opposite to me, i.e., *this is passed as the "left argument".


    Yes. I mean, if the operator does not have a left operand, then it just does not have a left operand, so it is a no brainer to deduce what the argument must be.
    You would think that, but if I place the "+" to the right, I get an error.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Niels_M
    You would think that, but if I place the "+" to the right, I get an error.
    So, what do you deduce from that?

    Or put it another way: what is the syntax for the unary operator+? Use some variant of BNF to describe it, if you will.
    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

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    What I deduce from that is that when overloading unary operators, then the right operand is passed by reference to the *this-pointer. Regarding what is being passed, I feel confident in my statement, even though you said it was the other way (Overloading operators).

    Am I correct?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Niels_M
    What I deduce from that is that when overloading unary operators, then the right operand is passed by reference to the *this-pointer.
    Yes, but note that some unary operators have left operands, e.g., postfix operator++. My point is: unary operator+ only has a right operand. There is nothing to talk about a left operand, so I cannot understand why you are worrying about this.

    Quote Originally Posted by Niels_M
    Regarding what is being passed, I feel confident in my statement, even though you said it was the other way
    You feel confident in stating that for binary operators, "the left argument is passed by reference to the *this-pointer"? I think we are just looking at it from different points of view. Your view is correct, but expressed as such, it sounds nonsensical to me because arguments are not passed to variables. Rather, the left argument is passed by reference to the function, and is used to initialise the first parameter. If the function is a member function, the first parameter is effectively *this.
    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

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    Yes, but note that some unary operators have left operands, e.g., postfix operator++. My point is: unary operator+ only has a right operand. There is nothing to talk about a left operand, so I cannot understand why you are worrying about this.
    So there is no way for me to overload an operator in C++ such that I can write e.g. +temp (temp is an object)?



    Quote Originally Posted by laserlight View Post
    You feel confident in stating that for binary operators, "the left argument is passed by reference to the *this-pointer"? I think we are just looking at it from different points of view. Your view is correct, but expressed as such, it sounds nonsensical to me because arguments are not passed to variables. Rather, the left argument is passed by reference to the function, and is used to initialise the first parameter. If the function is a member function, the first parameter is effectively *this.
    I agree with you; your view is probably better. I will use that.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Niels_M
    So there is no way for me to overload an operator in C++ such that I can write e.g. +temp (temp is an object)?
    Err... but you can do that. It is impossible to overload such that you can write temp+ where temp is an object and + is the unary operator+.
    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
    Quote Originally Posted by laserlight View Post
    Err... but you can do that. It is impossible to overload such that you can write temp+ where temp is an object and + is the unary operator+.
    Right, I meant to ask if there was a way to overload a unary operator in order to write temp+, but you answered that question. Thanks for clarifying.
    Last edited by Niels_M; 11-08-2010 at 10:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading unary operators using pointers
    By Niels_M in forum C++ Programming
    Replies: 21
    Last Post: 09-04-2010, 06:15 AM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  3. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  4. Problem with unary minus overloading
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 01-14-2005, 01:42 PM
  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