Thread: When is it sufficient ot leave auto generated copy /copy agignment operator?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    When is it sufficient ot leave auto generated copy /copy agignment operator?

    Hi

    When is it sufficient ot leave auto generated copy /copy assignment operator and when is it not? Could someone be so kind to post a small example for each situation since i don't quite get this ?? (it is not necessary for it to be a code just a conceptual case and what can go wrong)

    thank you so much


    baxy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider the member variables: do they all do their own memory management? Generally, if you see a (non-smart) pointer in there, it is an indicator that the class may need to define the copy constructor and copy assignment operator (or declare them private). Likewise, it is a hint that you may need to define the destructor (read up on the rule of three, though this rule has expanded somewhat with the advent of C++11).
    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
    Apr 2006
    Posts
    2,149
    Ideally the only time you need an explicit copy operations is when your class is a resource. That could mean it's a container that manages it's own memory, or a wrapper around a file or something fetched over a network. As laserlight says, a destructor is also likely needed.

    Raw pointers are an indicator of this; if a class has a raw pointer, it's probably managing whatever it points to as a resource. If the class does a lot of other things, it would be good design to separate the code that manages the resource into it's own class.


    Another case I've encountered that needs explicit copy constructors is when the class is copyable, but contains parts that cannot be copied. However, these cases are rare.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-13-2013, 07:36 PM
  2. Why not use an = Operator instead of a Copy Constructor?
    By thetinman in forum C++ Programming
    Replies: 48
    Last Post: 10-15-2007, 03:58 PM
  3. Template generated copy constructor
    By AverageSoftware in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2007, 10:51 AM
  4. Copy constructors and the = operator
    By Cactus_Hugger in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2007, 02:57 AM
  5. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM

Tags for this Thread