More Books
C++ Gotchas: Avoiding Common Problems in Coding and Design
Main Page
Table of content
Copyright
Addison-Wesley Professional Computing Series
Preface
Acknowledgments
Chapter 1. Basics
Gotcha #1: Excessive Commenting
Gotcha #2: Magic Numbers
Gotcha #3: Global Variables
Gotcha #4: Failure to Distinguish Overloading from Default Initialization
Gotcha #5: Misunderstanding References
Gotcha #6: Misunderstanding Const
Gotcha #7: Ignorance of Base Language Subtleties
Gotcha #8: Failure to Distinguish Access and Visibility
Gotcha #9: Using Bad Language
Gotcha #10: Ignorance of Idiom
Gotcha #11: Unnecessary Cleverness
Gotcha #12: Adolescent Behavior
Chapter 2. Syntax
Gotcha #13: Array/Initializer Confusion
Gotcha #14: Evaluation Order Indecision
Gotcha #15: Precedence Problems
Gotcha #16: 'for' Statement Debacle
Gotcha #17: Maximal Munch Problems
Gotcha #18: Creative Declaration-Specifier Ordering
Gotcha #19: Function/Object Ambiguity
Gotcha #20: Migrating Type-Qualifiers
Gotcha #21: Self-Initialization
Gotcha #22: Static and Extern Types
Gotcha #23: Operator Function Lookup Anomaly
Gotcha #24: Operator '->' Subtleties
Chapter 3. The Preprocessor
Gotcha #25: '#define' Literals
Gotcha #26: '#define' Pseudofunctions
Gotcha #27: Overuse of '#if'
Gotcha #28: Side Effects in Assertions
Chapter 4. Conversions
Gotcha #29: Converting through 'void *'
Gotcha #30: Slicing
Gotcha #31: Misunderstanding Pointer-to-Const Conversion
Gotcha #32: Misunderstanding Pointer-to-Pointer-to-Const Conversion
Gotcha #33: Misunderstanding Pointer-to-Pointer-to-Base Conversion
Gotcha #34: Pointer-to-Multidimensional-Array Problems
Gotcha #35: Unchecked Downcasting
Gotcha #36: Misusing Conversion Operators
Gotcha #37: Unintended Constructor Conversion
Gotcha #38: Casting under Multiple Inheritance
Gotcha #39: Casting Incomplete Types
Gotcha #40: Old-Style Casts
Gotcha #41: Static Casts
Gotcha #42: Temporary Initialization of Formal Arguments
Gotcha #43: Temporary Lifetime
Gotcha #44: References and Temporaries
Gotcha #45: Ambiguity Failure of 'dynamic_cast'
Gotcha #46: Misunderstanding Contravariance
Chapter 5. Initialization
Gotcha #47: Assignment/Initialization Confusion
Gotcha #48: Improperly Scoped Variables
Gotcha #49: Failure to Appreciate C++'s Fixation on Copy Operations
Gotcha #50: Bitwise Copy of Class Objects
Gotcha #51: Confusing Initialization and Assignment in Constructors
Gotcha #52: Inconsistent Ordering of the Member Initialization List
Gotcha #53: Virtual Base Default Initialization
Gotcha #54: Copy Constructor Base Initialization
Gotcha #55: Runtime Static Initialization Order
Gotcha #56: Direct versus Copy Initialization
Gotcha #57: Direct Argument Initialization
Gotcha #58: Ignorance of the Return Value Optimizations
Gotcha #59: Initializing a Static Member in a Constructor
Chapter 6. Memory and Resource Management
Gotcha #60: Failure to Distinguish Scalar and Array Allocation
Gotcha #61: Checking for Allocation Failure
Gotcha #62: Replacing Global New and Delete
Gotcha #63: Confusing Scope and Activation of Member 'new' and 'delete'
Gotcha #64: Throwing String Literals
Gotcha #65: Improper Exception Mechanics
Gotcha #66: Abusing Local Addresses
Gotcha #67: Failure to Employ Resource Acquisition Is Initialization
Gotcha #68: Improper Use of 'auto_ptr'
Chapter 7. Polymorphism
Gotcha #69: Type Codes
Gotcha #70: Nonvirtual Base Class Destructor
Gotcha #71: Hiding Nonvirtual Functions
Gotcha #72: Making Template Methods Too Flexible
Gotcha #73: Overloading Virtual Functions
Gotcha #74: Virtual Functions with Default Argument Initializers
Gotcha #75: Calling Virtual Functions in Constructors and Destructors
Gotcha #76: Virtual Assignment
Gotcha #77: Failure to Distinguish among Overloading, Overriding, and Hiding
Gotcha #78: Failure to Grok Virtual Functions and Overriding
Gotcha #79: Dominance Issues
Chapter 8. Class Design
Gotcha #80: Get/Set Interfaces
Gotcha #81: Const and Reference Data Members
Gotcha #82: Not Understanding the Meaning of Const Member Functions
Gotcha #83: Failure to Distinguish Aggregation and Acquaintance
Gotcha #84: Improper Operator Overloading
Gotcha #85: Precedence and Overloading
Gotcha #86: Friend versus Member Operators
Gotcha #87: Problems with Increment and Decrement
Gotcha #88: Misunderstanding Templated Copy Operations
Chapter 9. Hierarchy Design
Gotcha #89: Arrays of Class Objects
Gotcha #90: Improper Container Substitutability
Gotcha #91: Failure to Understand Protected Access
Gotcha #92: Public Inheritance for Code Reuse
Gotcha #93: Concrete Public Base Classes
Gotcha #94: Failure to Employ Degenerate Hierarchies
Gotcha #95: Overuse of Inheritance
Gotcha #96: Type-Based Control Structures
Gotcha #97: Cosmic Hierarchies
Gotcha #98: Asking Personal Questions of an Object
Gotcha #99: Capability Queries
Bibliography

Gotcha #77: Failure to Distinguish among Overloading, Overriding, and Hiding

It's always a shock to discover, many minutes into a technical discussion, that your interlocutor doesn't know the difference between overloading and overriding. Add a poor appreciation for block-structured hiding and you've got the makings of some really murky communication. Things don't have to be that way, however, and it's a cinch to distinguish among these concepts.

In C++, overloading is simply the use of the same function identifier for different functions declared in the same scope. That last proviso is important:

bool process( Credit & ); 
bool process( Acceptance & );
bool process( OrderForm & );

These three global functions are clearly overloaded. They share the process identifier and are declared in the same scope. The compiler will distinguish among them by the actual argument used in the call to process. That makes sense. If I ask to process an Acceptance, I'd expect the call to resolve to the second process function above, not the first or third. In C++, the name of a function is composed of a combination of the function's identifier (in this case, process) and the types of the formal arguments in the function's declaration. Let's embed these three functions in a class:

class Processor { 
 public:
   virtual ~Processor();
   bool process( Credit & );
   bool process( Acceptance & );
   bool process( OrderForm & );
   // . . .
};

They're still overloaded, and the compiler will still be able to perform the proper resolution based on the actual argument to the member function. The virtual destructor in the Processor class indicates that its designer intended it to be used as a base class, so we should feel free to extend its functionality through derivation:

class MyProcessor : public Processor { 
 public:
   bool process( Rejection & );
   // . . .
};

But not like this. The derived class process function doesn't additionally overload the base class process functions. It hides them:

Acceptance a; 
MyProcessor p;
p.process( a ); // error!

When the compiler looks up the name process in the scope of the derived class, it finds a single candidate function. This function is declared to take a Rejection formal argument, so we have an argument mismatch (unless there is some way to convert an Acceptance into a Rejection). End of discussion. The compiler will not continue its search for candidate process functions into enclosing scopes. The derived class process is declared in the scope of the derived class, not of the base class, and therefore can't overload the base class functions.

It's possible to import the base class declarations into the derived class scope with a using-declaration:

class MyProcessor : public Processor { 
 public:
   using Processor::process;
   bool process( Rejection & );
   // . . .
};

Now all four names are present in the same scope, and the derived class process function additionally overloads the three functions explicitly imported into the derived class scope. Note that this is not necessarily an exemplary design practice, because it's complex, and a complex design is always inferior to a simple design unless it has some compensating merit.

In this case, a Rejection can be processed only through the MyProcessor interface, and a compile-time error will result if we attempt to process a Rejection with the base class Processor interface. If a Rejection can be converted to an Acceptance, OrderForm, or Credit, however, the call will succeed under either interface but will exhibit different behavior.

Overriding can occur only in the presence of a base class virtual function. Period. Overriding has nothing whatsoever to do with overloading. A nonvirtual base class function cannot be overridden, only hidden:

class Doer { 
 public:
   virtual ~Doer();
   bool doit( Credit & );
   virtual bool doit( Acceptance & );
   virtual bool doit( OrderForm & );
   virtual bool doit( Rejection & );
   // . . .
};
class MyDoer : public Doer {
 private:
   bool doit( Credit & ); // #1, hides
   bool doit( Acceptance & ); // #2, overrides
   virtual bool doit( Rejection & ) const; // #3, doesn't override
   double doit( OrderForm & ); // #4, an error
   // . . .
};

(Please note that the Doer classes above are for illustration only and are not intended to exemplify good design practice. In particular, it's only rarely acceptable to overload virtual functions. See Gotcha #73.)

The doit function labeled #1 above does not override the corresponding base class function, because the base class function is not virtual. It does, however, hide all four base class doit functions.

The function labeled #2 does override the corresponding base class function. Note that access level has no effect on overriding. It doesn't matter that the base class function is public and the derived class function is private or vice versa. Conventionally, an overriding derived class function has the same access level as the corresponding base class function. In some cases, however, it may be desirable to depart from this standard practice:

class Visitor { 
 public:
   virtual void visit( Acceptance & );
   virtual void visit( Credit & );
   virtual void visit( OrderForm & );
   virtual int numHits();
};
class ValidVisitor : public Visitor {
   void visit( Acceptance & ); // overrides
   void visit( Credit & ); // overrides
   int numHits( int ); // #5, nonvirtual
};

In this case, the hierarchy designer has decided to allow customization of the base class behavior but would still like to require users of the hierarchy to employ the base class interface. The designer does this by declaring the base class member functions public but overriding them with private derived class functions.

Note also that the use of the keyword virtual in the derived class is purely optional when overriding a base class function. The meaning of the derived class function declaration is precisely the same with or without the presence of the keyword:

class MyValidVisitor : public ValidVisitor { 
   void visit( Credit & ); // overrides
   void visit( OrderForm & ); // overrides
   int numHits(); // #6, virtual, overrides Visitor::numHits
};

A common misconception is that the absence of the virtual keyword in an overriding derived class function will prevent that function from being over ridden itself in more derived classes. That is not the case, and MyValid Visitor::visit( Credit & ) overrides the corresponding functions in ValidVisitor and Visitor.

It's also perfectly valid for a derived class to override remote base class functions. MyValidVisitor::visit( OrderForm & ) overrides the corresponding function in Visitor.

It's even legal for a derived class to override a remote base class function that isn't visible in the scope of the derived class. For example, the function labeled #5, ValidVisitor::numHits, doesn't override the base class function Visitor::numHits, but it does hide the base class function from more derived classes. In spite of this hiding, the function MyValidVisitor::numHits does override Visitor::numHits.

The member function of MyDoer labeled #3 is subtle. It is virtual, but only because it's so declared. It doesn't actually override a base class virtual because it's a const member function, and the base class has no corresponding virtual const member function. Constness is part of the function signature (see Gotcha #82).

The member function of MyDoer labeled #4 above is in error. It overrides the corresponding base class virtual function, but it doesn't have a compatible return type; the base class function returns bool, and the derived class version returns double. This is a compile-time error.

In general, if a derived class function overrides a base class function, it must have the same return type. This is to ensure static type safety during runtime binding. A derived class virtual function is usually invoked through the base class function's interface (that's why we have virtual functions, after all). The compiler must generate code that assumes that the type of value returned from the function call—whether it's actually bound at runtime to the base class function or to an overriding derived class function—is the one declared in the base class.

In the case of the illegal function declaration labeled #4, the derived class function would attempt to copy an object that is sizeof(double) bytes into a location reserved for the return value of sizeof(bool) bytes. Even if the sizes are compatible (that is, bools are at least as big as doubles) it is unlikely a double, when interpreted as a bool, would give a consistently reasonable result.

This rule has an exception for what are known as "covariant return types." (Don't confuse covariance with contravariance. See Gotcha #46.) The return types of a base class member function and an overriding derived class function are covariant if they are pointers or references to classes and the class type in the derived class function return has an is-a relationship to the class type in the base class function return. That's a mouthful, so let's look at two canonical examples of covariant return types:

class B { 
   virtual B *clone() const = 0;
   virtual Visitor *genVisitor() const;
   // . . .
};
class D : public B {
   D *clone() const;
   ValidVisitor *genVisitor() const;
};

The clone function returns a pointer to a duplicate of the object making the clone request (this is an instance of the Prototype pattern; see Gotcha #76). Typically, the request is made through the base class interface, and the precise type of the cloned object is not known:

B *aB = getAnObjectDerivedFromB(); 
B *anotherLikeThat = aB->clone();

On occasion, however, we'll have more precise information about the type, and we'd like to avoid losing that information or forcing a downcast:

D *aD = getAnObjectThatIsAtLeastD(); 
D *anotherLikeThatD = aD->clone();

Without the covariant return, we'd be forced to downcast the return value from B * to D *:

D *anotherLikeThatD = static_cast<D *>(aD->clone()); 

Note that, in this case, we're able to use the efficient static_cast operator in preference to dynamic_cast, because we know that D's clone operation returns a D object. In other contexts the use of dynamic_cast (or avoiding a cast entirely) would be safer and preferable.

The genVisitor function (an instance of the Factory Method pattern; see Gotcha #90) illustrates that the classes in the covariant returns don't have to be related to the hierarchy in which the functions occur.

The overriding mechanism in C++ is a flexible and useful tool. However, this utility comes at the cost of some complexity. Other items in this chapter provide advice on how to tame the overriding mechanism's complexity while retaining the ability to exploit it as the need arises.