question about copy constructor and assignment operator

This is a discussion on question about copy constructor and assignment operator within the C++ Programming forums, part of the General Programming Boards category; Hi all, In which cases does the compiler forces us to have a copy constructor and an assignment operator? I ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    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
    20,975
    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.
    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

  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
    20,975
    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.
    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

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,266
    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
    3,820
    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
    20,975
    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.
    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

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, 11:44 AM
  2. Question on overloading the operator delete...
    By Darkinyuasha1 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2009, 10:50 AM
  3. operator overloading question
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2003, 09:03 PM
  4. & operator question
    By VanJay011379 in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2002, 06:13 PM
  5. quick c++ operator question
    By pkananen in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 05:46 PM

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