Thread: importance of returning reference in operator overloading.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by vaibhavs17
    why compiler did not flash error for your piece of code without &.
    Which compiler might that be, and what is the exact code?
    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

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Quote Originally Posted by vaibhavs17 View Post
    why compiler did not flash error for your piece of code without &.

    Code:
      friend std::ostream operator<<(std::ostream& out, const Integer& i)
        {
            return out << i.n;
        }
    Could you please tell me reason for this?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by vaibhavs17
    Could you please tell me reason for this?
    Maybe it is a compiler bug where your compiler determined that it did not need to copy, so it ignored the fact that copying was not possible.

    EDIT:
    I tested with:
    • Comeau online compiler
    • g++ 4.2.4
    • MinGW port of g++ 3.4.5
    • MSVC8

    and all of them reported at least one error, unlike your compiler.
    Last edited by laserlight; 05-13-2009 at 01:31 AM.
    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

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    [QUOTE=brewbuck;861946]Yes. It would cause the following code to not work as you might expect:

    Code:
    (a += 1) += 2;
    The expected result would be to add 3 to the value of a. But if operator+=() returns an object instead of a reference, it will actually only add 1 to the value, because the result of the first increment is not the original object but a temporary copy of it -- the 2 gets added to this temporary then silently vanishes.

    How can we achieve the same with below peice of code?
    without returning a reference, output is same with both case

    Code:
    #include <iostream.h>
    class Sample
    {
    	int i;
    public:
    	Sample(){}
    	Sample(int x)
    	{ i = x;
    	}
    
    	Sample operator+(const Sample& s1)
    	{
    		i = i + s1.i;
    		return *this;
    	}
    	void show()
    	{
    		cout<<i<<endl;
    	}
    };
    
    void main()
    {
    	Sample a(5),b(10),c(15);
    //	a.show();
    	//b.show();
    	a =a +b +c ;
    	a.show();
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by vaibhavs17
    How can we achieve the same with below peice of code?
    operator+= and operator+ are supposed to have different semantics (i.e., meaning), so trying to achieve the same strange thing as brewbuck's example does not quite make sense. Consequently, your operator+ is a poor one as it changes the current object.
    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

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Thanks, i would have to stop thread now. I understood now.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM