Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Part Five.  Operators


Chapter 26. What's Your Address?

Programmers who overload unary operator& should be sentenced to writing libraries that need to operate properly when fed such classes.

—Peter Dimov, esteemed Boost member, Boost newsgroup June 2002

Although that's very funny, it's also a pretty strong statement. Why is there such antipathy to the use of this operator?

In this chapter we look at some of the problems that overloading operator &() can cause. Our solution will be an unexciting one, simply the recommendation that you follow Peter's implicit advice, forswear any use of this overload for shortsighted gains, and avoid much grief down the line.

One thing I should make clear. Anytime in this chapter that I refer to operator &() it will be the unary form, which is the address of operator. The binary form, which is the bitwise OR operator, is an entirely different beast.



class Int


{


  Int operator &(Int const &);  // Bitwise operator


  void *operator &()            // Address-of operator


. . .




      Previous section   Next section