CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Red face Returning by reference

    Code:
    class Foo
    {
    	int data;
    
    public:
    	Foo (int n):data (n)
    	{
    	}
    
    	Foo ():data (0)
    	{		
    	}
    
    	Foo &operator= (int n)
    	{
    		data = n;
    		return *this;
    	}
    
    	Foo &operator= (const Foo& foo)
    	{
    		data = foo.data;
    		return *this;
    	}
    
    	void display ()
    	{
    		cout << "data = " << data << endl;
    	}
    };
    Why in the above code the operator= returns by reference? If I return it by value (Foo operator= ()) it seems to work fine. Do we return by refernce because it is faster (only the address of the object will be returned instead of making a copy of the object and then returning it, please correct me if I'm wrong )?

    Thanks.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Returning by reference

    To be able to do something like:
    Code:
    a = b = c;
    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Returning by reference

    Hi Albert,

    your assignment can be done with return by value operators, too. References are returned to modify the return value in a later expression:

    Code:
    // assign a + b to c, multiply c by d and store the result in c.
    (c = a + b) *= d;
    This cannot be done by returning by value, because it changes a temporary object only, leaving c untouched (after it has been assigned a+b), so you have to return by reference.

    Regards,
    Guido
    - Guido

  4. #4
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Returning by reference

    Hi Guido,

    Really? So, I was wrong!!! I thought the reference was necessary to make the last assignation
    Code:
    a=b=c;
    Thanks.

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  5. #5
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: Returning by reference

    Guys, thank you for the responses [].
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Returning by reference

    It's also for efficiency.

    Code:
    	Foo &operator= (const Foo& foo)
    	{
    		data = foo.data;
    		return *this;
    	}
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Returning by reference

    Quote Originally Posted by AlbertGM
    Hi Guido,

    Really? So, I was wrong!!! I thought the reference was necessary to make the last assignation
    Code:
    a=b=c;
    Thanks.

    Albert.
    Partly true, not wrong.
    Regards,
    Ramkrishna Pawar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured