Thread: Using references and *this?

  1. #1
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Question Using references and *this?

    Consider the following class.
    Code:
    class complex
    {
    private:
    int r,i;
    
    public:
        complex operator + (complex c2)
        {
            r = r + c2.r;
            i = i + c2.i;
            return *this;
        }
    ...
    };
    If I make a call in main as:
    Code:
    c3 = c1 + c2
    (assuming c1,c2,c3 are all declared and initialized)

    then, the overloaded function would return the object c3 itself, right?

    What if I change the function to return a reference?

    Code:
        complex& operator + (complex c2)
        {
            r = r + c2.r;
            i = i + c2.i;
            return *this;
        }
    What is the difference between the two? Does the first function cause a copy of the object pointed to by *this to be created and then copied back to c3? And what about in the latter case?Am I missing something here?
    Last edited by black_stallion; 03-06-2012 at 08:39 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by black_stallion
    then, the overloaded function would return the object c3 itself, right?
    No, it would return a copy of c1.

    Quote Originally Posted by black_stallion
    Does the first function cause a copy of the object pointed to by *this to be created and then copied back to c3?
    Conceptually, yes. However, the copy might be elided such that you only have the call to operator+ and copy assignment operator=.

    Quote Originally Posted by black_stallion
    And what about in the latter case?
    In the latter case, there is no copy returned, but then due to the copy assignment copying will still be done.
    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

  3. #3
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Quote Originally Posted by laserlight View Post
    Conceptually, yes. However, the copy might be elided such that you only have the call to operator+ and copy assignment operator=.
    Might be? Does that mean this copy is sometimes deleted and sometimes not(and thereby wasting memory if I make such repeated calls)?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by black_stallion View Post
    Might be? Does that mean this copy is sometimes deleted and sometimes not(and thereby wasting memory if I make such repeated calls)?
    The copy (if made) is on the stack, so no question of 'delete'. It is the time && space wasted, that counts.
    The standard allows the compilers, to optimize that copy out.

  5. #5
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Okay I see..Thanx!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    By the way, your implementation is horribly broken, because in 'c3 = c1 + c2', it actually modifies c1.

    The standard way to implement arithmetic operators is to first implement the compound assignment operator and then implement the normal operator in terms of that as a free function:
    Code:
    class complex {
      int r, i;
    public:
      complex &operator +=(complex other) {
        r += other.r;
        i += other.i;
        return *this;
      }
    };
    
    complex operator +(complex left, complex right) {
      complex tmp = left;
      tmp += right;
      return tmp;
    }
    I'm passing complex by value because it's small enough that it would usually be a good idea (depending on some details). Larger things, or things that aren't trivial to copy, should be passed by const reference.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Quote Originally Posted by CornedBee View Post
    By the way, your implementation is horribly broken, because in 'c3 = c1 + c2', it actually modifies c1.
    The standard way to implement arithmetic operators is to first implement the compound assignment operator and then implement the normal operator in terms of that as a free .
    Yes I realized that..I've already modified my code. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. References
    By Dae in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2009, 03:29 AM
  2. References
    By KPM in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2009, 10:54 PM
  3. References in C?
    By TriKri in forum C Programming
    Replies: 5
    Last Post: 07-16-2006, 04:39 PM
  4. VST References
    By StinkyRyan in forum C++ Programming
    Replies: 0
    Last Post: 02-08-2006, 03:51 PM
  5. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM

Tags for this Thread