views:

294

answers:

4

I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator?

+10  A: 

Factor out the common code to a private member function. A simple (rather contrived) example:

#include <iostream>

class Test
{
public:
  Test(const char* n)
  {
    name = new char[20];
    strcpy(name, n);
  }

  ~Test()
  {
    delete[] name;
  }

  // Copy constructor
  Test(const Test& t)
  {
    std::cout << "In copy constructor.\n";
    MakeDeepCopy(t);
  }

  // Assignment operator
  const Test& operator=(const Test& t)
  {
    std::cout << "In assignment operator.\n";
    MakeDeepCopy(t);
  }

  const char* get_name() const { return name; }

private:
  // Common function where the actual copying happens.
  void MakeDeepCopy(const Test& t)
  {        
    strcpy(name, t.name);
  }

private:
  char* name;
};

int
main()
{
  Test t("vijay");
  Test t2(t); // Calls copy constructor.
  Test t3(""); 
  t3 = t2; // Calls the assignment operator.

  std::cout << t.get_name() << ", " << t2.get_name() << ", " << t3.get_name() << '\n';

  return 0;
}
Vijay Mathew
+1 - this is almost always the best way to handle code duplication within a class.
Eric Petroelje
memory leak! MakeDeepCopy ignores the possibility of name already pointing to allocated memory.
sellibitze
But it's a pity that the copy constructor cannot use member initializers, this way...
xtofl
Note that `t2 = t2` won't work with the assignment operator as written. (You care about this if you want to sort an array of them, for example.)
Dave Hinton
To add a little more: I don't think reuse is applicable. There's a difference between creating a new object and just assigning to it. In the cases where simple member-wise assignment is sufficient, custom copy constructors and assignment operators are not needed. In case you have to do something more advanced (i.e. managing allocated memory) the similarity between copy and assignment is near zero.
sellibitze
@sellibitze Thanks! Fixed the leak.
Vijay Mathew
@sellibitze The only point of the example is to show that whatever behavior that is common to both copy constructor and the assignment operator should be moved to a common function. Ignore all other shortcomings of the code.
Vijay Mathew
@Vijay: but now you forgot to initialize "name" for copy construction which sort of proves my point. When explicit resource management is involved copy construction and copy assignment do different things. The possibility of reuse is minimal.
sellibitze
+7  A: 

There's no "general way" for writing custom copy constructors and assignment operators that works in all cases. But there's an idiom called "copy-&-swap":

 class myclass
 {
    ...
 public:
    myclass(myclass const&);

    void swap(myclass & with);

    myclass& operator=(myclass copy) {
        this->swap(copy);
        return *this;
    }

    ...
};

It's useful in many (but not all) situations. Sometimes you can do better. A vector or a string could have a better assignment which reuses allocated storage if it was large enough.

sellibitze
+1 - good and terse summary of copy-swap i think. Also good idea about reusing storage.
Johannes Schaub - litb
You might want to point out the subtlety here between your operator= and the more standard const myclass
Bill
The swap() should probably marked as nothrow. Ditto on Bills comment. Some more explnation would be nice.
Martin York
Matthieu M.
How does this prevents code duplication since `swap` must be specialized?
gregseth
It's not exactly "code duplication" since initialization is not the same as assignment. But Matthieu is right. You obviously have to "enumerate" the members in your swap function. I see no way around this unless you implement your copy ctor in terms of default constructed members + assignment. But in my opinion, this isn't a good solution. Ideally, you don't have to write copy ctor and assignment yourself and ideally, you don't have classes with 99 members.
sellibitze
+4  A: 
My &My::operator = (My temp)  // thanks, sellibitze
{
    swap (*this, temp);
    return *this;
}

and implement a specialised std::swap<> (My &, My &).

Dave Hinton
sellibitze
As Dave notices, alexandrescu found about about this 6 years ago already: http://www.ddj.com/cpp/184403855 . Interesting read too!
Johannes Schaub - litb
errm, incidentally the poster is also called Dave. I'm sorry about the confusion - i meant the Dave Abrahams of cpp-next :)
Johannes Schaub - litb
That Alexandrescu article was really helpful.
Dan Hook
+1  A: 

As has already been pointed out by quite a few posters, having operator= create a new object with the copy constructor and then use swap is the common technique used to not have to replicate code in operator=.

That said, I want to point out some a pro and a con of this technique to help you decide whether it is appropriate.

Pro - exception safety

If your object has resource requirements that could cause a throw and assuming that swap will not throw, this technique provides the strong guarantee of exception safety (either the object being assigned to has taken on the value of the other object or it is unchanged).

Con - resource footprint

An issue with this technique is that it requires a complete new object to be created before the old one is released. If your object requires a lot of resources, this can be a problem.

R Samuel Klatchko