Thread: Why not use an = Operator instead of a Copy Constructor?

  1. #46
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    BTW, brewbuck, does the copied_ptr<> you use require the class to implement a clone() function or something similar? How does it do with base and derived classes?
    It requires that the underlying type be copy-constructible, that's about it. If the copy constructor of the wrapped type does the right thing, then copied_ptr<> does the right thing. If the wrapped type has some oddity, such as requiring cloning to be done through a clone() method, then I usually invent a wrapper specific to that type. But it doesn't happen often.

    A far less common but equally useful wrapper is not_copied<>, which does nothing at all in its copy constructor and assignment operator. I use that when I specifically DO NOT want a particular member to be copied when the object is copied. For instance, if it has a const member. When a const member is present, the compiler can't generate a default copy constructor or assignment operator, forcing you to write one yourself -- unless you wrap the type up in a not_copied<>.

  2. #47
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would imagine that copied_ptr<> doesn't work too well on inheritance hierarchies. For example, if you had a copied_ptr<Base> then it would slice the object during copy, right?

    I was looking for a smart pointer a while ago that implemented C++ style copy semantics and worked with polymorphic classes, but only found some that required a clone() style function.

  3. #48
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    I would imagine that copied_ptr<> doesn't work too well on inheritance hierarchies. For example, if you had a copied_ptr<Base> then it would slice the object during copy, right?
    Yeah, it would. I didn't see what you were getting at originally. I do have a cloned_ptr<> template which invokes clone(). Like everyone else, I haven't found a better way...

    EDIT: Actually, that isn't accurate. My cloned_ptr<> template actually takes two parameters -- the type of the object itself, and a "cloner" type which is responsible for cloning the object. This type defaults to a default_cloner<> which just calls the object's clone() method. But if the object doesn't HAVE a clone() method, you can write an appropriate replacement which clones it and use that as the cloner type. So it's a bit more flexible, as it doesn't require the name of the clone method to be "clone()".
    Last edited by brewbuck; 10-15-2007 at 03:56 PM.

  4. #49
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thank you, very interesting. to be honest, most of my c++ classed had private copy constructors and assignment operators so far. But I think I'd feel a bit more comfortable now If I've to use them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 05:35 PM
  2. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 06:27 PM
  3. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  4. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 05:17 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM