DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Assignment Operator, Returning const reference

  1. #1
    Join Date
    Feb 2004
    Posts
    9

    Assignment Operator, Returning const reference

    Ok, so let's say I have the following class:

    Code:
    class A
    {
    public:
        A();
        A(int data) : m_data(data) {}
        const A& operator=(const A& rhs);
    
    private:
        int m_data;
    };
    At first glance, I would assume that returning a const reference from
    an overloaded assignment operator would generate compiler errors
    if you attempted to chain assignments, say a1=a2=a3, but to my surprise,
    I tried it and the compiler did not complain. My question is, what are the consequences of returning a const reference from an assignment operator rather than just a regular reference?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    In this case it doesn't matter much because when youi have a=b; you actually have
    a.operator=(b); where b is const anyway, and a isn't. However, when the function call returns, a is returned is a reference to const, which is then used as the argument of another operator= call, which in turn takes a reference to const argument.
    So in this case, returning a reference to const isn't a problem but it could become a problem in other scenarios, e.g, when the resulting reference is assigned to a non-const reference:

    A& ref=(a=b); //error
    Danny Kalev

  3. #3
    Join Date
    Feb 2004
    Posts
    9
    Ok, thanks, that makes more sense.

    I guess I wasn't thinking right about how a chained assignment works.

  4. #4
    Join Date
    Jan 2006
    Posts
    55
    Just in case you want to know why we need to return const reference--
    To avoid extra copying and an implicit call to the copy constructor, we make the return value a constant reference parameter,

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by Nokia2280
    Just in case you want to know why we need to return const reference--
    To avoid extra copying and an implicit call to the copy constructor, we make the return value a constant reference parameter,
    In the case of an assignment operator, this isn't exactly the reason. You simply have no other choice since operator= must return a reference, never a value object. The question is whether the return value should reference to const or plain reference. Normally, it's the former.
    Danny Kalev

  6. #6
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,

    I just made a li'l experiment and changed the return type of my assignment operator from 'ref' to 'object'. It still compiles and when stepping through, the operator is still called! Is it (again) VS6.0 playing up or what happens here? Is it expected, that the operator is called but the result is something other than I would expect?

    D

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    It will work but it could create serious bugs such as assigning a reference to a dangling object:

    A & ref= b=c;

    Which would bind a temporay (i.e., the object returned by value from operator=) to ref.
    Danny Kalev

Similar Threads

  1. Screen Capture Bug
    By Ranger in forum VB Classic
    Replies: 3
    Last Post: 05-29-2006, 09:43 PM
  2. Screen Capture Bug
    By Ranger in forum VB Classic
    Replies: 0
    Last Post: 12-09-2000, 01:21 PM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center