Thread: Passing by a reference AND copy constructor

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    135

    Passing by a reference AND copy constructor

    Hey everyone.

    Here, in the second answer:
    c++ - Why must the copy assignment operator return a reference/const reference? - Stack Overflow

    'Alex Collins' gives this example:

    Code:
    A operator=(const A& rhs) { /* ... */ }; 
    a = b = c; // calls assignment operator twice, calls copy constructor twice, calls destructor type to delete the temporary values! Very wasteful and nothing gained!
    To demonstrate why it's not efficient to return by value instead of by reference.

    But, in the comment he writes something I didn't expect to:
    "calls copy constructor twice"

    But I thought so far that if the argument receives the value by reference as in the example above, the copy constructor is not called at all.
    That's why we actually strive to send args by reference, not to call the copy constructor, not to create another copy.
    So, how is it correct that in his example, the copy constructor is called at all?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,912
    This copy assignment operator returns a copy, and this copy is a new object, so the copy constructor would be called to create it. Since the copy assignment operator is invoked twice, the copy constructor is therefore invoked at least twice.
    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
    Join Date
    Nov 2019
    Posts
    135
    @laserlight - Got it.
    BTW, Why do you say "at least" and not precisely?
    Thank you!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,912
    It's possible to use the copy constructor to implement the copy assignment operator, in which case the copy constructor would be called four times for this particular implementation.
    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

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @laserlight -
    Sounds reasonable.
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 51
    Last Post: 02-09-2009, 05:35 PM
  2. When would you need to use a copy constructor
    By steve1_rm in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2008, 03:28 PM
  3. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 05:35 PM
  4. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2002, 02:23 PM
  5. Copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 02:02 PM

Tags for this Thread