Tutorial :How often do you implement the big three?



Question:

I was just musing about the number of questions here that either are about the "big three" (copy constructor, assignment operator and destructor) or about problems caused by them not being implemented correctly, when it occurred to me that I could not remember the last time I had implemented them myself. A swift grep on my two most active projects indicate that I implement all three in only one class out of about 150.

That's not to say I don't implement/declare one or more of them - obviously base classes need a virtual destructor, and a large number of my classes forbid copying using the private copy ctor & assignment op idiom. But fully implemented, there is this single lonely class, which does some reference counting.

So I was wondering am I unusual in this? How often do you implement all three of these functions? Is there any pattern to the classes where you do implement them?


Solution:1

I think that it's rare that you need all three. Most classes that require an explicit destructor aren't really suitable for copying.

It's just better design to use self-destructing members (which normally don't require things like copy-construction) than a big explicit destructor.


Solution:2

I rarely implement them, but often declare them private (copy constructors and assignemt operators, that is).


Solution:3

Like you, almost never.

But I'm not tied to the STL approach of programming where you copy everything in and around in containers - usually if it's not a primitive, I'll use a pointer, smart or otherwise.

I mainly use RAII patterns, thus avoid writing destructors. Although, I do put empty bodies in my .cc file to help keep code bloat down.

And, like you, I'll declare them private and unimplemented to prevent any accidental invoking.


Solution:4

It really depends on what type of problems you are working on. I have been working on a new project for the past few months and I think every class inherits from boost::noncopyable. Nine months ago I worked on a different project that used PODs quite a bit and I leveraged automatic copy ctor and assignment operator. If you are using boost::shared_ptr (and you should be), it should be rare to write your own copy ctor or assignment operator nowadays.


Solution:5

Most of the time, hardly ever. This is because the members that are used (reference based smart ptr, etc) already implement the proper semantics, or the object is non-copyable.

A few patterns come up when I find myself implementing these:

  1. destructive copy , i.e. move pattern like auto_ptr or lock
  2. dispose pattern which hardly every comes up in C++, but I've used it about three times in my career (and just a week ago in fact)
  3. pimpl pattern, where the pimpl is fwd declared in the header, and managed by a smart ptr. Then the empty dtor goes in the .cc file but still classifies as "not complier generated"

And one other trivial one that prints "I was destroyed" when I think I might have a circular reference somewhere and just want to make sure.


Solution:6

Any class that owns some pointers members need to define this three operations to implement deep copy (See here for a deep description).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »