TITLE: pros/cons of custom constructors/assignment operators (Newsgroups: comp.lang.c++.moderated,comp.lang.c++, 26 May 99) BORLA: "Anthony Borla" The reason that your code did not work is because you had no 'default constructor', so the array could not be built. A class should also include, as a minimum, the following: * A destructor, especially if memory is being allocated by the class. * Also, both a copy constructor and assignment operator should be included. These are important when passing the class as parameters or returning them from functions. They become vital if the class is allocating memory. CLAMAGE: Steve Clamage I sometimes see these recommendations, but have never understood why they should be considered good advice. The compiler creates a destructor, copy constructor, and copy- assignment operator for you if you don't declare them. In the case where the compiler-generated version has the correct semantics, you will often subvert compiler optimizations if you write your own. You also have more functions to maintain, increasing the risk of getting out of synchronization. You can also turn a POD-class into a non-POD class, eliminating some uses of the class. There are cases where you must provide your own versions of these functions. If the class manages a resource, you almost certainly must declare your own copy constructor, copy-assignment operator, and destructor. (They might be made private and never defined if you want to prohibit the operation.) If you intend to allow the class to be derived from, you almost certainly must provide a virtual destructor. One reason given for writing your own redundant functions is so that maintainers of the class will know that you didn't just forget about them. You can achieve that result with a comment in the class. The default constructor (one that can be invoked with no arguments) is a special case. If you declare no constructors, the compiler will create one for you. Otherwise, you must define one if you want to allow its use. Not every class logically needs a default constructor; in some cases, it makes no sense. Given that initialization should be preferred to assignment in general, a design might well require that every object of a given type be initialized with values for which there is no reasonable default.