The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Overloading operator&() in C++
by Christopher Diggins
April 9, 2005
Summary
My colleague Matthew Wilson made a post recently suggesting that overloading operator&() should be avoided. While I agree with much of what he says, I would like to suggest a valid use for overloading operator&() which enables writing safer code.

Advertisement

In response to Matthew Wilson's recent post at the C++ Source about overloading operator&(), I would like to submit a valid use case. Consider the following code:

  SomeObject o;
  SomeObject* p;
  p = &o;
  delete p; // dumb but legal
I know you are thinking that the code example is simply asinine, but it is intended as a purely artificial example of what can and does happen in real world scenarios. This is due to what I percieve as a flaw in the C++ type system, which has only one kind of pointer. If you think about it from, it does seem silly for a strongly typed language to allow you to so easily delete stack allocated objects. This can be easily fixed by writing some trivial pointer classes which I call unsmart pointers.

You can use an undeletable unsmart pointer to represent the return type of a call to operator&(). An unsmart pointer is a pointer class which does not automatically destroy the resource, and undeletable means that the pointer does not provide any method to delete the resource it references.

Consider the following implementation of SomeObject which overloads operator&().

  #include <ootl/ootl_ptr.hpp>

  struct SomeObject {
    typedef ootl::ptr<SomeObject, !ootl::deletable> undeletable_ptr;
    undeletable_ptr operator&() {
      return this;
    }
  };

  int main() {
    SomeObject o;
    SomeObject* p;
    p = &o; // compiler error; undeletable pointers can not be assigned to raw pointers.
    delete p;
    return 0;
  }
Today I just sent in my final revision on make next installment of Agile C++ for the C++ Users Journal, Unsmart Pointers Part 1, which not surprisingly covers undeletable and their complement deletable pointers in more depth.

You can download the unsmart pointer source code as part of the most recent ootl release at http://www.ootl.org or view the source online at http://www.ootl.org/ootl/ootl_ptr.hpp.htm.

I would like to point out that even if I disagree on this small point that I own and heartily recommend Matthew's Book Imperfect C++.

Talk Back!

Have an opinion? Be the first to post a comment about this weblog entry.

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use