More Effective C++ | Chapter 1: Basics Back to Introduction Continue to Item 1: Distinguish between pointers and references. Basics Ah, the basics. Pointers, references, casts, arrays, constructors you can't get much more basic than that. All but the simplest C++ programs use most of these features, and many programs use them all. In spite of our familiarity with these parts of the language, sometimes they can still surprise us. This is especially true for programmers making the transition from C to C++, because the concepts behind references, dynamic casts, default constructors, and other non-C features are usually a little murky. This chapter describes the differences between pointers and references and offers guidance on when to use each. It introduces the new C++ syntax for casts and explains why the new casts are superior to the C-style casts they replace. It examines the C notion of arrays and the C++ notion of polymorphism, and it describes why mixing the two is an idea whose time will never come. Finally, it considers the pros and cons of default constructors and suggests ways to work around language restrictions that encourage you to have one when none makes sense. By heeding the advice in the items that follow, you'll make progress toward a worthy goal: producing software that expresses your design intentions clearly and correctly. Back to Basics Continue to Item 2: Prefer C++-style casts Item 1: Distinguish between pointers and references. Pointers and references look different enough (pointers use the "*" and "->" operators, references use "."), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other? First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must always refer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference. "But wait," you wonder, "what about underhandedness like this?" char *pc = 0; // set pointer to null char& rc = *pc; // make reference refer to // dereferenced null pointer Well, this is evil, pure and simple. The results are undefined (compilers can generate output to do anything they like), and people who write this kind of code should be shunned until they agree to cease and desist. If you have to worry about things like this in your software, you're probably best off avoiding references entirely. Either that or finding a better class of programmers to work with. We'll henceforth ignore the possibility that a reference can be "null." Because a reference must refer to an object, C++ requires that references be initialized: string& rs; // error! References must // be initialized string s("xyzzy"); string& rs = s; // okay, rs refers to s Pointers are subject to no such restriction: string *ps; // uninitialized pointer: // valid but risky The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That's because there's no need to test the validity of a reference before using it: void printDouble(const double& rd) { cout << rd; // no need to test rd; it } // must refer to a double Pointers, on the other hand, should generally be tested against null: void printDouble(const double *pd) { if (pd) { // check for null pointer cout << *pd; } } Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized: string s1("Nancy"); string s2("Clancy"); string& rs = s1; // rs refers to s1 string *ps = &s1; // ps points to s1 rs = s2; // rs still refers to s1, // but s1's value is now // "Clancy" ps = &s2; // ps now points to s2; // s1 is unchanged In general, you should use a pointer whenever you need to take into account the possibility that there's nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you're referring to that object, you'll never want to refer to anything else. There is one other situation in which you should use a reference, and that's when you're implementing certain operators. The most common example is operator[]. This operator typically needs to return something that can be used as the target of an assignment: vector v(10); // create an int vector of size 10; // vector is a template in the // standard C++ library (see Item 35) v[5] = 10; // the target of this assignment is // the return value of operator[] If operator[] returned a pointer, this last statement would have to be written this way: *v[5] = 10; But this makes it look like v is a vector of pointers, which it's not. For this reason, you'll almost always want operator[] to return a reference. (For an interesting exception to this rule, see Item 30.) References, then, are the feature of choice when you know you have something to refer to, when you'll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers. Back to Item 1: Distinguish between pointers and references Continue to Item 3: Never treat arrays polymorphically Item 2: Prefer C++-style casts. Consider the lowly cast. Nearly as much a programming pariah as the goto, it nonetheless endures, because when worse comes to worst and push comes to shove, casts can be necessary. Casts are especially necessary when worse comes to worst and push comes to shove. Still, C-style casts are not all they might be. For one thing, they're rather crude beasts, letting you cast pretty much any type to pretty much any other type. It would be nice to be able to specify more precisely the purpose of each cast. There is a great difference, for example, between a cast that changes a pointer-to-const-object into a pointer-to-non-const-object (i.e., a cast that changes only the constness of an object) and a cast that changes a pointer-to-base-class-object into a pointer-to-derived-class-object (i.e., a cast that completely changes an object's type). Traditional C-style casts make no such distinctions. (This is hardly a surprise. C-style casts were designed for C, not C++.) A second problem with casts is that they are hard to find. Syntactically, casts consist of little more than a pair of parentheses and an identifier, and parentheses and identifiers are used everywhere in C++. This makes it tough to answer even the most basic cast-related questions, questions like, "Are any casts used in this program?" That's because human readers are likely to overlook casts, and tools like grep cannot distinguish them from non-cast constructs that are syntactically similar. C++ addresses the shortcomings of C-style casts by introducing four new cast operators, static_cast, const_cast, dynamic_cast, and reinterpret_cast. For most purposes, all you need to know about these operators is that what you are accustomed to writing like this, (type) expression you should now generally write like this: static_cast(expression) For example, suppose you'd like to cast an int to a double to force an expression involving ints to yield a floating point value. Using C-style casts, you could do it like this: int firstNumber, secondNumber; ... double result = ((double)firstNumber)/secondNumber; With the new casts, you'd write it this way: double result = static_cast(firstNumber)/secondNumber; Now there's a cast that's easy to see, both for humans and for programs. static_cast has basically the same power and meaning as the general-purpose C-style cast. It also has the same kind of restrictions. For example, you can't cast a struct into an int or a double into a pointer using static_cast any more than you can with a C-style cast. Furthermore, static_cast can't remove constness from an expression, because another new cast, const_cast, is designed specifically to do that. The other new C++ casts are used for more restricted purposes. const_cast is used to cast away the constness or volatileness of an expression. By using a const_cast, you emphasize (to both humans and compilers) that the only thing you want to change through the cast is the constness or volatileness of something. This meaning is enforced by compilers. If you try to employ const_cast for anything other than modifying the constness or volatileness of an expression, your cast will be rejected. Here are some examples: class Widget { ... }; class SpecialWidget: public Widget { ... }; void update(SpecialWidget *psw); SpecialWidget sw; // sw is a non-const object, const SpecialWidget& csw = sw; // but csw is a reference to // it as a const object update(&csw); // error! can't pass a const // SpecialWidget* to a function // taking a SpecialWidget* update(const_cast(&csw)); // fine, the constness of &csw is // explicitly cast away (and // csw and sw may now be // changed inside update) update((SpecialWidget*)&csw); // same as above, but using a // harder-to-recognize C-style cast Widget *pw = new SpecialWidget; update(pw); // error! pw's type is Widget*, but // update takes a SpecialWidget* update(const_cast(pw)); // error! const_cast can be used only // to affect constness or volatileness, // never to cast down the inheritance // hierarch By far the most common use of const_cast is to cast away the constness of an object. The second specialized type of cast, dynamic_cast, is used to perform safe casts down or across an inheritance hierarchy. That is, you use dynamic_cast to cast pointers or references to base class objects into pointers or references to derived or sibling base class objects in such a way that you can determine whether the casts succeeded.1 Failed casts are indicated by a null pointer (when casting pointers) or an exception (when casting references): Widget *pw; ... update(dynamic_cast(pw)); // fine, passes to update a pointer // to the SpecialWidget pw points to // if pw really points to one, // otherwise passes the null pointer void updateViaRef(SpecialWidget& rsw); updateViaRef(dynamic_cast(*pw)); // fine, passes to updateViaRef the // SpecialWidget pw points to if pw // really points to one, otherwise // throws an exception dynamic_casts are restricted to helping you navigate inheritance hierarchies. They cannot be applied to types lacking virtual functions (see also Item 24), nor can they cast away constness: int firstNumber, secondNumber; ... double result = dynamic_cast(firstNumber)/secondNumber; // error! no inheritance is involved const SpecialWidget sw; ... update(dynamic_cast(&sw)); // error! dynamic_cast can't cast // away constness If you want to perform a cast on a type where inheritance is not involved, you probably want a static_cast. To cast constness away, you always want a const_cast. The last of the four new casting forms is reinterpret_cast. This operator is used to perform type conversions whose result is nearly always implementation-defined. As a result, reinterpret_casts are rarely portable. The most common use of reinterpret_cast is to cast between function pointer types. For example, suppose you have an array of pointers to functions of a particular type: typedef void (*FuncPtr)(); // a FuncPtr is a pointer // to a function taking no // args and returning void FuncPtr funcPtrArray[10]; // funcPtrArray is an array // of 10 FuncPtrs Let us suppose you wish (for some unfathomable reason) to place a pointer to the following function into funcPtrArray: int doSomething(); You can't do what you want without a cast, because doSomething has the wrong type for funcPtrArray. The functions in funcPtrArray return void, but doSomething returns an int: funcPtrArray[0] = &doSomething; // error! type mismatch A reinterpret_cast lets you force compilers to see things your way: funcPtrArray[0] = // this compiles reinterpret_cast(&doSomething); Casting function pointers is not portable (C++ offers no guarantee that all function pointers are represented the same way), and in some cases such casts yield incorrect results (see Item 31), so you should avoid casting function pointers unless your back's to the wall and a knife's at your throat. A sharp knife. A very sharp knife. If your compilers lack support for the new casting forms, you can use traditional casts in place of static_cast, const_cast, and reinterpret_cast. Furthermore, you can use macros to approximate the new syntax: #define static_cast(TYPE,EXPR) ((TYPE)(EXPR)) #define const_cast(TYPE,EXPR) ((TYPE)(EXPR)) #define reinterpret_cast(TYPE,EXPR) ((TYPE)(EXPR)) You'd use the approximations like this: double result = static_cast(double, firstNumber)/secondNumber; update(const_cast(SpecialWidget*, &sw)); funcPtrArray[0] = reinterpret_cast(FuncPtr, &doSomething); These approximations won't be as safe as the real things, of course, but they will simplify the process of upgrading your code when your compilers support the new casts. There is no easy way to emulate the behavior of a dynamic_cast, but many libraries provide functions to perform safe inheritance-based casts for you. If you lack such functions and you must perform this type of cast, you can fall back on C-style casts for those, too, but then you forego the ability to tell if the casts fail. Needless to say, you can define a macro to look like dynamic_cast, just as you can for the other casts: #define dynamic_cast(TYPE,EXPR) (TYPE)(EXPR) Remember that this approximation is not performing a true dynamic_cast; there is no way to tell if the cast fails. I know, I know, the new casts are ugly and hard to type. If you find them too unpleasant to look at, take solace in the knowledge that C-style casts continue to be valid. However, what the new casts lack in beauty they make up for in precision of meaning and easy recognizability. Programs that use the new casts are easier to parse (both for humans and for tools), and they allow compilers to diagnose casting errors that would otherwise go undetected. These are powerful arguments for abandoning C-style casts, and there may also be a third: perhaps making casts ugly and hard to type is a good thing. Back to Item 2: Prefer C++-style casts Continue to Item 4: Avoid gratuitous default constructors Item 3: Never treat arrays polymorphically. One of the most important features of inheritance is that you can manipulate derived class objects through pointers and references to base class objects. Such pointers and references are said to behave polymorphically as if they had multiple types. C++ also allows you to manipulate arrays of derived class objects through base class pointers and references. This is no feature at all, because it almost never works the way you want it to. For example, suppose you have a class BST (for binary search tree objects) and a second class, BalancedBST, that inherits from BST: class BST { ... }; class BalancedBST: public BST { ... }; In a real program such classes would be templates, but that's unimportant here, and adding all the template syntax just makes things harder to read. For this discussion, we'll assume BST and BalancedBST objects contain only ints. Consider a function to print out the contents of each BST in an array of BSTs: void printBSTArray(ostream& s, const BST array[], int numElements) { for (int i = 0; i < numElements; ++i) { s << array[i]; // this assumes an } // operator<< is defined } // for BST objects This will work fine when you pass it an array of BST objects: BST BSTArray[10]; ... printBSTArray(cout, BSTArray, 10); // works fine Consider, however, what happens when you pass printBSTArray an array of BalancedBST objects: BalancedBST bBSTArray[10]; ... printBSTArray(cout, bBSTArray, 10); // works fine? Your compilers will accept this function call without complaint, but look again at the loop for which they must generate code: for (int i = 0; i < numElements; ++i) { s << array[i]; } Now, array[i] is really just shorthand for an expression involving pointer arithmetic: it stands for *(array+i). We know that array is a pointer to the beginning of the array, but how far away from the memory location pointed to by array is the memory location pointed to by array+i? The distance between them is i*sizeof(an object in the array), because there are i objects between array[0] and array[i]. In order for compilers to emit code that walks through the array correctly, they must be able to determine the size of the objects in the array. This is easy for them to do. The parameter array is declared to be of type array-of-BST, so each element of the array must be a BST, and the distance between array and array+i must be i*sizeof(BST). At least that's how your compilers look at it. But if you've passed an array of BalancedBST objects to printBSTArray, your compilers are probably wrong. In that case, they'd assume each object in the array is the size of a BST, but each object would actually be the size of a BalancedBST. Derived classes usually have more data members than their base classes, so derived class objects are usually larger than base class objects. We thus expect a BalancedBST object to be larger than a BST object. If it is, the pointer arithmetic generated for printBSTArray will be wrong for arrays of BalancedBST objects, and there's no telling what will happen when printBSTArray is invoked on a BalancedBST array. Whatever does happen, it's a good bet it won't be pleasant. The problem pops up in a different guise if you try to delete an array of derived class objects through a base class pointer. Here's one way you might innocently attempt to do it: // delete an array, but first log a message about its // deletion void deleteArray(ostream& logStream, BST array[]) { logStream << "Deleting array at address " << static_cast(array) << '\n'; delete [] array; } BalancedBST *balTreeArray = // create a BalancedBST new BalancedBST[50]; // array ... deleteArray(cout, balTreeArray); // log its deletion You can't see it, but there's pointer arithmetic going on here, too. When an array is deleted, a destructor for each element of the array must be called (see Item 8). When compilers see the statement delete [] array; they must generate code that does something like this: // destruct the objects in *array in the inverse order // in which they were constructed for ( int i = the number of elements in the array - 1; i >= 0; --i) { array[i].BST::~BST(); // call array[i]'s } // destructor Just as this kind of loop failed to work when you wrote it, it will fail to work when your compilers write it, too. The language specification says the result of deleting an array of derived class objects through a base class pointer is undefined, but we know what that really means: executing the code is almost certain to lead to grief. Polymorphism and pointer arithmetic simply don't mix. Array operations almost always involve pointer arithmetic, so arrays and polymorphism don't mix. Note that you're unlikely to make the mistake of treating an array polymorphically if you avoid having a concrete class (like BalancedBST) inherit from another concrete class (such as BST). As Item 33 explains, designing your software so that concrete classes never inherit from one another has many benefits. I encourage you to turn to Item 33 and read all about them. Back to Item 3: Never treat arrays polymorphically Continue to Operators Item 4: Avoid gratuitous default constructors. A default constructor (i.e., a constructor that can be called with no arguments) is the C++ way of saying you can get something for nothing. Constructors initialize objects, so default constructors initialize objects without any information from the place where the object is being created. Sometimes this makes perfect sense. Objects that act like numbers, for example, may reasonably be initialized to zero or to undefined values. Objects that act like pointers ( Item 28) may reasonably be initialized to null or to undefined values. Data structures like linked lists, hash tables, maps, and the like may reasonably be initialized to empty containers. Not all objects fall into this category. For many objects, there is no reasonable way to perform a complete initialization in the absence of outside information. For example, an object representing an entry in an address book makes no sense unless the name of the thing being entered is provided. In some companies, all equipment must be tagged with a corporate ID number, and creating an object to model a piece of equipment in such companies is nonsensical unless the appropriate ID number is provided. In a perfect world, classes in which objects could reasonably be created from nothing would contain default constructors and classes in which information was required for object construction would not. Alas, ours is not the best of all possible worlds, so we must take additional concerns into account. In particular, if a class lacks a default constructor, there are restrictions on how you can use that class. Consider a class for company equipment in which the corporate ID number of the equipment is a mandatory constructor argument: class EquipmentPiece { public: EquipmentPiece(int IDNumber); ... }; Because EquipmentPiece lacks a default constructor, its use may be problematic in three contexts. The first is the creation of arrays. There is, in general, no way to specify constructor arguments for objects in arrays, so it is not usually possible to create arrays of EquipmentPiece objects: EquipmentPiece bestPieces[10]; // error! No way to call // EquipmentPiece ctors EquipmentPiece *bestPieces = new EquipmentPiece[10]; // error! same problem There are three ways to get around this restriction. A solution for non-heap arrays is to provide the necessary arguments at the point where the array is defined: int ID1, ID2, ID3, ..., ID10; // variables to hold // equipment ID numbers ... EquipmentPiece bestPieces[] = { // fine, ctor arguments EquipmentPiece(ID1), // are provided EquipmentPiece(ID2), EquipmentPiece(ID3), ..., EquipmentPiece(ID10) }; Unfortunately, there is no way to extend this strategy to heap arrays. A more general approach is to use an array of pointers instead of an array of objects: typedef EquipmentPiece* PEP; // a PEP is a pointer to // an EquipmentPiece PEP bestPieces[10]; // fine, no ctors called PEP *bestPieces = new PEP[10]; // also fine Each pointer in the array can then be made to point to a different EquipmentPiece object: for (int i = 0; i < 10; ++i) bestPieces[i] = new EquipmentPiece( ID Number ); There are two disadvantages to this approach. First, you have to remember to delete all the objects pointed to by the array. If you forget, you have a resource leak. Second, the total amount of memory you need increases, because you need the space for the pointers as well as the space for the EquipmentPiece objects. You can avoid the space penalty if you allocate the raw memory for the array, then use "placement new" (see Item 8) to construct the EquipmentPiece objects in the memory: // allocate enough raw memory for an array of 10 // EquipmentPiece objects; see Item 8 for details on // the operator new[] function void *rawMemory = operator new[](10*sizeof(EquipmentPiece)); // make bestPieces point to it so it can be treated as an // EquipmentPiece array EquipmentPiece *bestPieces = static_cast(rawMemory); // construct the EquipmentPiece objects in the memory // using "placement new" (see Item 8) for (int i = 0; i < 10; ++i) new (&bestPieces[i]) EquipmentPiece( ID Number ); Notice that you still have to provide a constructor argument for each EquipmentPiece object. This technique (as well as the array-of-pointers idea) allows you to create arrays of objects when a class lacks a default constructor; it doesn't show you how to bypass required constructor arguments. There is no way to do that. If there were, it would defeat the purpose of constructors, which is to guarantee that objects are initialized. The downside to using placement new, aside from the fact that most programmers are unfamiliar with it (which will make maintenance more difficult), is that you must manually call destructors on the objects in the array when you want them to go out of existence, then you must manually deallocate the raw memory by calling operator delete[] (again, see Item 8): // destruct the objects in bestPieces in the inverse // order in which they were constructed for (int i = 9; i >= 0; --i) bestPieces[i].~EquipmentPiece(); // deallocate the raw memory operator delete[](rawMemory); If you forget this requirement and use the normal array-deletion syntax, your program will behave unpredictably. That's because the result of deleting a pointer that didn't come from the new operator is undefined: delete [] bestPieces; // undefined! bestPieces // didn't come from the new // operator For more information on the new operator, placement new and how they interact with constructors and destructors, see Item 8. The second problem with classes lacking default constructors is that they are ineligible for use with many template-based container classes. That's because it's a common requirement for such templates that the type used to instantiate the template provide a default constructor. This requirement almost always grows out of the fact that inside the template, an array of the template parameter type is being created. For example, a template for an Array class might look something like this: template class Array { public: Array(int size); ... private: T *data; }; template Array::Array(int size) { data = new T[size]; // calls T::T() for each ... // element of the array } In most cases, careful template design can eliminate the need for a default constructor. For example, the standard vector template (which generates classes that act like extensible arrays) has no requirement that its type parameter have a default constructor. Unfortunately, many templates are designed in a manner that is anything but careful. That being the case, classes without default constructors will be incompatible with many templates. As C++ programmers learn more about template design, this problem should recede in significance. How long it will take for that to happen, however, is anyone's guess. The final consideration in the to-provide-a-default-constructor-or-not-to-provide-a-default-constructor dilemma has to do with virtual base classes (see Item E43). Virtual base classes lacking default constructors are a pain to work with. That's because the arguments for virtual base class constructors must be provided by the most derived class of the object being constructed. As a result, a virtual base class lacking a default constructor requires that all classes derived from that class no matter how far removed must know about, understand the meaning of, and provide for the virtual base class's constructors' arguments. Authors of derived classes neither expect nor appreciate this requirement. Because of the restrictions imposed on classes lacking default constructors, some people believe all classes should have them, even if a default constructor doesn't have enough information to fully initialize objects of that class. For example, adherents to this philosophy might modify EquipmentPiece as follows: class EquipmentPiece { public: EquipmentPiece( int IDNumber = UNSPECIFIED); ... private: static const int UNSPECIFIED; // magic ID number value // meaning no ID was }; // specified This allows EquipmentPiece objects to be created like this: EquipmentPiece e; // now okay Such a transformation almost always complicates the other member functions of the class, because there is no longer any guarantee that the fields of an EquipmentPiece object have been meaningfully initialized. Assuming it makes no sense to have an EquipmentPiece without an ID field, most member functions must check to see if the ID is present. If it's not, they'll have to figure out how to stumble on anyway. Often it's not clear how to do that, and many implementations choose a solution that offers nothing but expediency: they throw an exception or they call a function that terminates the program. When that happens, it's difficult to argue that the overall quality of the software has been improved by including a default constructor in a class where none was warranted. Inclusion of meaningless default constructors affects the efficiency of classes, too. If member functions have to test to see if fields have truly been initialized, clients of those functions have to pay for the time those tests take. Furthermore, they have to pay for the code that goes into those tests, because that makes executables and libraries bigger. They also have to pay for the code that handles the cases where the tests fail. All those costs are avoided if a class's constructors ensure that all fields of an object are correctly initialized. Often default constructors can't offer that kind of assurance, so it's best to avoid them in classes where they make no sense. That places some limits on how such classes can be used, yes, but it also guarantees that when you do use such classes, you can expect that the objects they generate are fully initialized and are efficiently implemented. Back to Introduction Continue to Chapter 2: Operators 1 A second, unrelated use of dynamic_cast is to find the beginning of the memory occupied by an object. We explore that capability in Item 27. Return