TITLE: the law of the big three PROBLEM: "Julian R. Parramore" In a large nearly finished project I want to force the use of the law of the big three, i.e. all classes to have a copy ctor, assignment operator, and virtual dtor. All classes are derived from a single base class. I thought the law said that IF a class needed any one of the three, THEN it needed all three. RESPONSE: kanze@gabi-soft.fr (J. Kanze), 16 Nov 95 Actually, I think that there are two distinct `laws' involved: 1. If a class needs one of the following, it needs all of them: copy constructor, assignment operator and destructor. 2. If a class is to be derived from, it needs a virtual destructor. This has nothing to do with the precedent (and an empty virtual destructor doesn't count for requiring the other two in rule 1). PROBLEM: tlcltd@ibm.net (Julian R. Parramore) I'm wanting some (non-trivial) counter examples of where you wouldn't want to use the law of the big three. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 16 Nov 95 The "law" of the big three is not really a law, but a design guideline due to Marshall Cline and Greg Lomow. It says that if you have any of a destructor, copy constructor, or assignment operator in a given class, you should have all three. You can't let a design guideline do your thinking for you. The purpose of this one is to serve as a reminder. The compiler will supply default versions of these three member functions for you; if you need a special version of one of them, it is likely you need special versions of all three. If you write just one or two, you should think very carefully about why you are not writing all three. Here is one example where you don't need all three: You have an abstract base class which contains no data. It therefore does not need a user- written copy constructor or assignment operator -- there is nothing for them to do. But it does need a virtual destructor, because any class intended to be a base class should have a virtual destructor. The destructor is empty, of course. On the other hand, you could put in an empty copy constructor and assignment operator anyway, just to show that you didn't forget about them. It doesn't cause any extra overhead. [ original question: ... force the use of the law of the big three ... ] RESPONSE: kcline@sun132.spd.dsccc.com (Kevin Cline), 16 Nov 95 By "have a copy ctor" do you mean that a class has declared a copy ctor, or that a class has implemented a copy ctor? Since all the classes are derived from a single base class, (really really ALL of them?) making the base class destructor virtual will ensure that every derived class also has a virtual destructor, whether or not it is declared virtual. [ original question: ... I thought the law said that IF a class needed any one of the three, THEN it needed all three. ] That doesn't sound right either. Classes that are intended to be derived from should have virtual destructors. This decision is orthogonal to the decision about implementing or not implementing the copy constructor and assignment operator. Copy constructors and assignment operators have similar functionality, and if one is implemented then the other should also be implemented. Nearly every class should declare the copy constructor and assignment operator unless the compiler generated versions are sufficient. But they can be private and unimplemented, and therefore inaccessible. Copy constructors and assignment operators are inappropriate for many classes; one example is a class which represents an file open for reading (like istream). Scott Meyers wrote an article for the C++ Report last year discussing which member functions should be implemented for EVERY class. He concluded that the only member function needed by every class was the destructor.