Thread: question about copy constructor and assignment operator

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    question about copy constructor and assignment operator

    Hi all,

    In which cases does the compiler forces us to have a copy constructor and an assignment operator? I mean, when we have to define both explicitly.

    Thanks in advance

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Never? The whole point of writing either of those things is that you decide if you need them to do something different than what the compiler gives you, because there are different kinds of copies.

    The compiler assumes that it can just assign all the members like this when you use assignment:

    foo = rhs.foo;
    bar = rhs.bar;

    and so on. Memory management is the most common reason this will not be appropriate and then you will need to write your own assignment operator. For instance, an object may have raw pointers as members that should be handled. They teach that stuff in class.

    Writing a copy constructor is usually a prerequisite for implementing assignment which looks like this:

    Code:
    object & operator = (object & lhs , const object & rhs)
    {
       return object(lhs).swap(rhs);
    }
    where swap is a member function that swaps two objects. It may be the easiest way but it is not the only way, of course.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Most of what you said I already know, but I was asked that question and couldn't really find a case and still can't. Maybe it was a trick question.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, whiteflags' example is a little flawed in that, at least with respect to the current version of the C++ standard, an overloaded copy assignment operator must be a non-static member function. Furthermore, swap functions typically have a void return type. As such, you are more likely to see this:
    Code:
    object& object::operator=(object rhs)
    {
        swap(rhs);
        return *this;
    }
    or:
    Code:
    object& object::operator=(const object& rhs)
    {
        object temp(rhs);
        swap(temp);
        return *this;
    }
    A check for this != &rhs might be performed.
    Last edited by laserlight; 04-20-2010 at 08:40 AM. Reason: Oops: cannot swap with a const.
    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
    Oct 2009
    Posts
    46
    Well, thanks for the replies whiteflags and laserlight. Regarding the question maybe I didn't understand it correctly, because I can't find any answer/hints to it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sugarfree
    Regarding the question maybe I didn't understand it correctly, because I can't find any answer/hints to it.
    You might find these interesting:
    Quote Originally Posted by C++03 Section 12.8 Paragraph 7
    An implicitly-declared copy constructor is implicitly defined if it is used to initialize an object of its class type from a copy of an object of its class type or of a class type derived from its class type. [Note: the copy constructor is implicitly defined even if the implementation elided its use. ] A program is ill-formed if the class for which a copy constructor is implicitly defined has:
    • a nonstatic data member of class type (or array thereof) with an inaccessible or ambiguous copy constructor, or
    • a base class with an inaccessible or ambiguous copy constructor.
    Quote Originally Posted by C++03 Section 12.8 Paragraph 12
    An implicitly-declared copy assignment operator is implicitly defined when an object of its class type is assigned a value of its class type or a value of a class type derived from its class type. A program is ill-formed if the class for which a copy assignment operator is implicitly defined has:
    • a nonstatic data member of const type, or
    • a nonstatic data member of reference type, or
    • a nonstatic data member of class type (or array thereof) with an inaccessible copy assignment operator, or
    • a base class with an inaccessible copy assignment operator.
    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

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    By the way, whiteflags' example [...] must be a non-static member function.
    You're both crazy. The "rhs" should not be constant.

    Soma
    Last edited by phantomotap; 04-20-2010 at 08:36 AM. Reason: This, by the way, is a joke.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by phantomotap
    The "rhs" should not be constant.
    Heheh, oh yeah, otherwise we'll be trying to swap with something that is const. Fixed.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic operator overloading question
    By Swerve in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2009, 12:44 PM
  2. Question on overloading the operator delete...
    By Darkinyuasha1 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2009, 11:50 AM
  3. operator overloading question
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2003, 09:03 PM
  4. quick c++ operator question
    By pkananen in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 06:46 PM