Like Tree1Likes
  • 1 Post By CornedBee

Using references and *this?

This is a discussion on Using references and *this? within the C++ Programming forums, part of the General Programming Boards category; Consider the following class. Code: class complex { private: int r,i; public: complex operator + (complex c2) { r = ...

  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 07:39 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    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.
    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 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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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,893
    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.
    manasij7479 likes this.
    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, 02:51 PM
  5. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 07:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21